简体   繁体   English

Rails,has_many,belongs_to

[英]Rails, has_many, belongs_to

Schema: 架构:

  create_table "reports", :force => true do |t|
    t.integer  "user_id"
    t.string   "apparatus"
    t.string   "capt"
    t.text     "body"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", :force => true do |t|
    t.string   "login",                     :limit => 40
    t.string   "name",                      :limit => 100, :default => ""
    t.string   "email",                     :limit => 100
    t.string   "crypted_password",          :limit => 40
    t.string   "salt",                      :limit => 40
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "remember_token",            :limit => 40
    t.datetime "remember_token_expires_at"
    t.string   "rank"
    t.integer  "shift"
    t.integer  "access"
  end

user model: 用户模型:

Class User < ActiveRecord::Base
    has_many :reports
#   bunch of other stuff thats not important
end

report model: 报告模型:

Class Report < ActiveRecord::Base
    belongs_to :user
end

views/reports/index 意见/报告/指数

<% @reports.each do |report| %>
  <tr>
    <td><%= report.user_id %></td>  #     ****THIS IS THE LINE IN QUESTION****
    <td><%= report.apparatus %></td>
    <td><%= report.capt %></td>
    <td><%= report.body %></td>
    <td><%= link_to 'Show', report %></td>
    <td><%= link_to 'Edit', edit_report_path(report) %></td>
    <td><%= link_to 'Destroy', report, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>

I would like to be able to display the name of the user that created the report. 我希望能够显示创建报告的用户的名称。 I was under the assumption that declaring the belongs_to and has_many associations would make this possible by writing report.user.name or something like that. 我假设声明belongs_to和has_many关联可以通过编写report.user.name或类似的东西来实现这一点。 Where have I gone wrong? 我哪里出错了?

I'm 99% sure it's because one or more of your reports do not have an associated user. 我99%肯定是因为您的一个或多个报告没有相关用户。 Try 尝试

<%= report.user.name rescue "none" %>

When there is no value in user_id field on a report then report.user will return nil. 如果报表上的user_id字段中没有值,则report.user将返回nil。 So report.user.name would be like calling nil.name , which raises an error. 所以report.user.name就像调用nil.name ,这会引发错误。

UPDATE: here's a better way: 更新:这是一个更好的方法:

<%= report.user.try(:name) %>

You can do: 你可以做:

<%= report.user.name %>

But for efficiency, in your controller you can do a join to get the users name in the same query used to fetch @reports. 但是为了提高效率,您可以在控制器中进行连接,以获取用于获取@reports的同一查询中的用户名。

This query might look something like: 此查询可能类似于:

@reports = Report.select("reports.id as id, reports.apparatus as apparatus, reports.capt as capt, reports.body as body, users.name as user_name").joins("LEFT JOIN `users` ON `users`.`id` = `reports`.`user_id`")

Then your output would look like: 然后你的输出看起来像:

<%= report.user_name %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM