我在任务和优先级之间有一个简单的一对多关系。Rails - 在表循环中访问一个到多个变量
class Task < ActiveRecord::Base
attr_accessible :subject, :body, :priority_id, :company_id, :status_id, :user_ids
has_and_belongs_to_many :users
belongs_to :priority
belongs_to :company
belongs_to :status
end
class Priority < ActiveRecord::Base
attr_accessible :name
has_many :tasks
end
从我的任务/ show.html.erb视图
<%= @task.priority.name %>
这工作。
在我的任务然而/ index.html.erb
<% @tasks.each do |task| %>
<tr>
<td><%= task.subject %></td>
<td><%= task.body %></td>
<td><%= task.priority.name %></td>
<td><%= link_to 'Show', task %></td>
<td><%= link_to 'Edit', edit_task_path(task) %></td>
<td><%= link_to 'Destroy', task, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
到task.priority.name调用不起作用。
它抛出一个NoMethodError。
我猜这是一个非常愚蠢的新手错误,但我不能为我的生活弄清楚。它会假定Active Record魔法会过滤成这样的样子。
优秀的猜测Christoph!非常感谢 - 我想当你是新手时,你怀疑你的语法,不要想像这样简单的事情:)非常感谢 –