我有一个公司课程:
class Company < ActiveRecord::Base
validates :name, :presence => true
has_many :employees
end
以及员工只能与一家公司关联的员工类:
class Employee < ActiveRecord::Base
validates :lastName, :presence => true
belongs_to :company
validates :company, :presence => true
end
当我列出员工时,
<% @employees.each do |employee| %>
<tr>
<td><%= employee.firstName %></td> <- works
<td><%= employee.lastName %></td> <- works
<td><%= employee.company.name %></td> <- Get an 'undefined method `name' for nil:NilClass' error
</tr>
<% end %>
我认为员工公司已被急切地加载,因此我可以直接在对象中访问关联,还是我的语法错误?
任何帮助,将不胜感激