Project

inverse_of

0.01
No commit activity in last 3 years
No release in over 3 years
Adds the :inverse option to Active Record associations for Active Record 2.3.0 -- 2.3.5.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

Inverse Of

Backport of ActiveRecord 2.3.6's inverse associations feature.

class Parent < ActiveRecord::Base
  has_one :child, :inverse_of => :parent
end

class Child < ActiveRecord::Base
  belongs_to :parent, :inverse_of => :child
end

Now:

parent = Parent.first
parent.child.parent.equal?(parent)  # true

Without inverse associations, the last line is false. Although ActiveRecord does perform database query caching, you still suffer the overhead of creating unnecessary ActiveRecord objects.

Inverse associations are also necessary to support validations on the parent which must fire before the parent is saved. For example:

class Parent < ActiveRecord::Base
  has_one :child
  accepts_nested_attributes_for :child
end

class Child < ActiveRecord::Base
  belongs_to :parent
  validates_presence_of :parent_id
end

Parent.new(:child_attributes => {:name => 'child name'}).valid?

Here, the parent object fails validation, because the parent_id is not set yet. If inverses are declared, and the validation runs on the association rather than the ID, the validation passes.

class Parent < ActiveRecord::Base
  has_one :child, :inverse_of => :parent
  accepts_nested_attributes_for :child
end

class Child < ActiveRecord::Base
  belongs_to :parent
  validates_presence_of :parent
end

Note that running the Child validations will now require loading the Parent if it's not already loaded. On the upside, it will now also validate that the parent_id actually points to an existing record. However, you may wish to define a custom validation to check only the ID if the association is not set to avoid the extra database hit.

Inverse Of supports has_one, has_many, and belongs_to associations, including polymorphic belongs_to. Behavior should exactly match Rails 2.3.6; please file any differences as a bug.

Contributing

Copyright

Copyright (c) 2009-2010 George Ogata, released under the MIT license