我在任務和優先級之間有一個簡單的一對多關係。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!非常感謝 - 我想當你是新手時,你懷疑你的語法,不要想像這樣簡單的事情:)非常感謝 –