我有一种情况,我想在保存父对象之前访问关联的祖 parent 。我可以想到几个 hack,但我正在寻找一种干净的方法来完成此操作。以下面的代码为例说明我的问题:
class Company < ActiveRecord::Base
has_many :departments
has_many :custom_fields
has_many :employees, :through => :departments
end
class Department < ActiveRecord::Base
belongs_to :company
has_many :employees
end
class Employee < ActiveRecord::Base
belongs_to :department
delegate :company, :to => :department
end
company = Company.find(1) # => <Company id: 1>
dept = company.departments.build # => <Department id: nil, company_id: 1>
empl = dept.employees.build # => <Employee id: nil, department_id: nil>
empl.company # => Employee#company delegated to department.company, but department is nil
我使用的是 Rails 3.2.15。我明白这里发生了什么,我明白为什么 empl.department_id 是零;尽管我希望 Rails 在调用保存之前直接引用预期的关联,这样最后一行就可以通过未保存的部门对象进行委托(delegate)。有干净的解决方法吗?
更新:我也在 Rails 4 中尝试过,这是一个控制台 session :
2.0.0-p247 :001 > company = Company.find(1)
Company Load (1.5ms) SELECT "companies".* FROM "companies" WHERE "companies"."id" = ? LIMIT 1 [["id", 1]]
=> #<Company id: 1, name: nil, created_at: "2013-10-24 03:36:11", updated_at: "2013-10-24 03:36:11">
2.0.0-p247 :002 > dept = company.departments.build
=> #<Department id: nil, name: nil, company_id: 1, created_at: nil, updated_at: nil>
2.0.0-p247 :003 > empl = dept.employees.build
=> #<Employee id: nil, name: nil, department_id: nil, created_at: nil, updated_at: nil>
2.0.0-p247 :004 > empl.company
RuntimeError: Employee#company delegated to department.company, but department is nil: #<Employee id: nil, name: nil, department_id: nil, created_at: nil, updated_at: nil>
2.0.0-p247 :005 > empl.department
=> nil
更新 2:这是一个 test project on github .
最佳答案
请查看 belongs_to
的 :inverse_of
选项和 has_many
.在不同情况下构建和获取关联记录时,此选项将处理双向分配。
来自 Bi-directional associations对于文档中的 ActiveRecord::Associations::ClassMethods
:
Specifying the
:inverse_of
option on associations lets you tell Active Record about inverse relationships and it will optimise object loading.
关于ruby-on-rails - 如何通过尚未保存的父关联访问 ActiveRecord 祖父关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19553008/