我有三个模型:
class Company < ActiveRecord::Base
has_many :employees
has_many :dogs, :through => :employees
end
class Employee < ActiveRescord::Base
belongs_to :company
has_many :dogs
end
class Dog < ActiveRecord::Base
belongs_to :employee
delegate :id, :to => :employee, :prefix => true, :allow_nil => true
end
这工作得很好,在我看来,我可以调用 dog.employee_id。但是,如果我想在 RailsAdmin 中创建一个新实例(而不是在编辑现有对象时),我会收到以下错误:
RuntimeError at /dog/new
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
:allow_nil 设置为 true 并且其他属性的委托工作正常。有什么问题,我该如何解决?