저는 작업과 우선 순위 사이에 간단한 일대 다 관계를 가지고 있습니다. 내 작업/show.html.erb보기이 작동Rails - 테이블 루프에서 일대 다 변수에 액세스하기
<%= @task.priority.name %>
에서
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
. 내 작업에서 그러나
/<% @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 %>
index.html.erb task.priority.name에 대한 호출이 작동하지 않습니다.
NoMethodError를 발생시킵니다.
나는 이것이 정말로 어리지만 초보자가 실수라고 생각한다. 그러나 나는 내 인생을 생각할 수 없다. 액티브 레코드 (Active Record) 마법이 이런 모습으로 필터링되었을 것이라고 추정했을 것입니다.
뛰어난 추측 Christoph! 많은 감사합니다 - 당신이 당신의 구문을 의심하고 이것과 같은 간단한 것들을 생각하지 않아 초보자 일 것 같아요 :) 많이 감사합니다 –