ruby-on-rails - 如何通过尚未保存的父关联访问 ActiveRecord 祖父关联?

标签 ruby-on-rails ruby activerecord

我有一种情况,我想在保存父对象之前访问关联的祖 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/

相关文章:

ruby-on-rails - Rails ActiveRecord : pretty errors when deleting dependent entities with foreign keys constraints

ruby-on-rails - carrierwave gem 可以同时在 AWS-S3 和本地文件系统上存储文件吗?

ruby-on-rails - 如何在 ruby​​ on rails 应用程序中使用 postgresql "inner join"?

ruby-on-rails - 存储日期和可选的月/日

ruby - 在instance_eval中定义方法

activerecord - yii2 行为 ActiveRecord::EVENT_BEFORE_INSERT 不起作用

codeigniter - 从查询中获取结果以插入到其他使用事件记录中

ruby-on-rails - 使用 Rails 4 中的设备登录 API 生成 token

ruby-on-rails - 在 ruby​​ on rails 中查找关联中的所有 nil

ruby - 如何通过 http 使用 Ruby 访问 URL 并读取输出?