What Changed in Rails v2.3.6 – finally we got inverse_of

RubyOnRails 15 八月 2010 | View Comments


In this commitgithub.com/rails/rails/commit/5374fb3cad2970c95aa9d294d006b5804b760754#diff-12 we found we big improvement.

They add :inverse_of for active_record association

What is that used for?

Here is the example from commit message

class Man < ActiveRecord::Base
  has_one :face, :inverse_of => :man
end

class Face < ActiveRecord::Base
  belongs_to :man, :inverse_of => :face
end

m = Man.first
f = m.face

If we don’t have :inverse_of, f.man will be different object, not m@ object and what's more active_record will fire another sql query if you can @f.man.

After we add :inverse_of option, @ m == f.man , and @f.man will not fire a sql query.

But now :inverse_of only works for has_one, has_many. It also supplies inverse support for belongs_to
associations where the inverse is a has_one and it’s not a polymorphic.

I think DataMapper need change their website why DataMapper. Active Record has Identity Map now

Thanks h-lame for giving us this very useful feature which also have sufficient test.



Tagged in , , ,

blog comments powered by Disqus

Ads Plugin created by Jake Ruston's Wordpress Plugins - Powered by and football database.