6

我有一种情况,我想在保存父对象之前访问关联的祖父母。我可以想到几个黑客,但我正在寻找一种干净的方法来完成这个。以以下代码为例说明我的问题:

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 为 nil;虽然我希望 Rails 在调用 save 之前直接引用预期关联,这样最后一行可以通过未保存的部门对象委托。有干净的工作吗?

更新:我也在 Rails 4 中尝试过,这是一个控制台会话:

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:这是github 上的一个测试项目

4

3 回答 3

5

请查看和的:inverse_of选项。在不同情况下构建和获取关联记录时,此选项将处理双向分配。belongs_tohas_many

来自文档中的双向关联:ActiveRecord::Associations::ClassMethods

指定:inverse_of关联选项可以让你告诉 Active Record 反向关系,它会优化对象加载。

于 2013-10-24T19:24:19.367 回答
1

我不喜欢这个解决方案,但这似乎解决了问题:

empl = dept.employees.build { |e| e.association(:department).target = dept}

事实证明,您可以传递一个块来构建,ActiveRecord 将让步给具有新创建记录的块。谁知道这样会带来什么 ActiveRecord 怪异。我暂时把这个问题留着,看看是否有更好的解决方案。

于 2013-10-24T18:50:19.917 回答
0

在一些控制台实验之后——你可以说employee.department.company,即使部门还没有保存。department_id可能为零,但department关联存在。

2.0.0-p195 :041 > c = Company.create 
   (0.4ms)  begin transaction
  SQL (0.9ms)  INSERT INTO "companies" DEFAULT VALUES
   (486.4ms)  commit transaction
 => #<Company id: 4, department: nil, custom_fields: nil> 
2.0.0-p195 :042 > d = c.departments.build
 => #<Department id: nil, company_id: 4, employee_id: nil> 
2.0.0-p195 :043 > e = d.employees.build
 => #<Employee id: nil, department_id: nil> 
2.0.0-p195 :044 > e.department === d
 => true 
2.0.0-p195 :045 > e.department.company === c
 => true

编辑:所以,这在另一个干净的 Rails 4 应用程序的不同机器上不起作用。但是,它仍然可以在我的笔记本电脑上运行......也在一个干净的 Rails 4 应用程序中。让我们试着找出有什么不同!

e.method(:department)
=> #<Method: Employee(Employee::GeneratedFeatureMethods)#department> 

e.method(:department).source_location
=> ["/home/neil/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-  
    4.0.0/lib/active_record/associations/builder/association.rb", 69] 

这将我们带到这里:

def define_readers
  mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
    def #{name}(*args)
      association(:#{name}).reader(*args)
    end
  CODE
end

毫不奇怪,这定义了一个名为:department

def department *args
  association(:department).reader(*args)
end

如果它存在,则该调用reader只返回我们的关联@target,或者如果它手头有一个id,则尝试读取它。在我的情况下,@target设置为部门dtarget=要发现设置@target的点,我们可以截取ActiveRecord::Associations::Association

class ActiveRecord::Associations::Association 
  alias :_target= :target=
  def target= t
    puts "#{caller} set the target!"
    _target = t
  end
end

现在当我们打电话时,d.employees.build我们得到这个......

"/home/neil/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.0/lib/active_record/associations/association.rb:112:in `set_inverse_instance'", 
"/home/neil/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.0/lib/active_record/associations/collection_association.rb:376:in `add_to_target'",
"/home/neil/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.0/lib/active_record/associations/collection_association.rb:114:in `build'"

set_inverse_instance正在检查invertible_for?(record), (record我们的新 Employee 实例在哪里。)这只是调用reflection.inverse_of,并且必须返回一个真实值才能设置目标。

def inverse_of
  return unless inverse_name

  @inverse_of ||= klass.reflect_on_association inverse_name
end

所以让我们尝试一下...

2.0.0-p195 :055 > Employee.reflect_on_association :department
 => #<ActiveRecord::Reflection::AssociationReflection:0xa881788 @macro=:belongs_to, @name=:department, @scope=nil, @options={}, @active_record=Employee(id: integer, department_id: integer), @plural_name="departments", @collection=false, @class_name="Department", @foreign_key="department_id"> 

那不是零,所以当我调用时,@target 将在我的关联中设置d.employee.build,所以我可以调用e.department,等等。那么为什么它在这里非零,但对你来说是零(在我的另一台机器上?)如果我打电话Employee.reflections,我会得到以下信息:

> Employee.reflections
 => {:department=>#<ActiveRecord::Reflection::AssociationReflection:0x9a04598 @macro=:belongs_to, @name=:department, @scope=nil, @options={}, @active_record=Employee(id: integer, department_id: integer), @plural_name="departments", @collection=false, @class_name="Department", @foreign_key="department_id">} 

这是belongs_to方法的产物——如果你看,它就在那里。那么为什么(在你的情况下)没有set_inverse_instance找到它?

于 2013-10-23T22:09:09.177 回答