What are the inverse_of limitations in rails 3 with ActiveRecord

I read about inverse_of, and everything I see on the Internet seems inconsistent and confuses me. If you look here , you can see

There are several limitations to supporting inverse_of:

  • They do not work with: through associations.
  • They do not work with: polymorphic associations.
  • They do not work with: as associations.
  • For belongs_to associations, the reverse has_many associations are ignored.

but right above that, they give this example

class Customer < ActiveRecord::Base has_many :orders, :inverse_of => :customer end class Order < ActiveRecord::Base belongs_to :customer, :inverse_of => :orders end 

I think they say that the first inverse_of does nothing, but if so, why did they do it?

Also, while the above says inverse_of doesn't work through associations, this page says

If you use role_to in the connection model, it is recommended to set the option: inverse_of> to role_to, which will mean that the following example works correctly, where> tags is has_many: through union):

and gives this example

 @post = Post.first @tag = @post.tags.build :name => "ruby" @tag.save 

The last line should save the recorded record (Taggable). This will only work if set to>: inverse_of:

 class Taggable < ActiveRecord::Base belongs_to :post belongs_to :tag, :inverse_of => :taggings end 

It all seems inconsistent and very confusing to me. But in general, I see no harm, just saying the opposite in all respects. This is problem? I saw how many people ask about it, and have not seen a single solid yes or no.

+7
source share
1 answer

There is no filtering device that would always point it, and this will allow the rails to optimize the loading of objects so that you can move up and down the chain of relations of the active record in both directions without getting strange errors when changing the value on one object does not change its link .

The only problem with setting up where it does not work is related to certain types, as you said earlier, and if they fail, you will get cheating errors if you make a mistake in the method chain after changing something.

this is from rails APIs

 d = Dungeon.first t = d.traps.first d.level == t.dungeon.level # => true d.level = 10 d.level == t.dungeon.level # => false 

The Dungeon d and t.dungeon instances in the above example refer to the same object data from the database, but actually represent different copies of this data in memory. Specifying the option: inverse_of in associations allows you to inform Active Record of feedback relationships and optimize object loading.

+1
source

All Articles