Interesting Things

  • has_many and belongs_to associations can now automatically create back references each other, thanks to a Backport of :inverse_of from Rails 3 to rails 2.3.6. This allows us to keep our object graphs more correct and avoid situations where we have 2 copies of the same object because the object graph is walked in reverse. Here's how to use it:
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

Comments

  1. funny_falcon funny_falcon on August 04, 2010 at 03:17PM

    I wrote simple “identity map” capable to do job of :inverse_of. Also, it helps to save memory by updating attributes of already fetched objects, instead of creating new instances.

    gem is ar-simple-idmap repository: http://github.com/funny-falcon/identity-map