what are the limitations of inverse_of in rails 3 with ActiveRecord -
i've been reading inverse_of , i'm seeing online seems inconsistent , confuses me. if here, can see
there few limitations inverse_of support:
- they not work :through associations.
- they not work :polymorphic associations.
- they not work :as associations.
- for belongs_to associations, has_many inverse associations ignored.
yet right above that, give 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're saying first inverse_of nothing, if why did it?
in addition, though above thing says inverse_of doesn't work through assocations, this page says
if using belongs_to on join model, idea set :inverse_of >option on belongs_to, mean following example works correctly >tags has_many :through association):
and gives example
@post = post.first @tag = @post.tags.build :name => "ruby" @tag.save
the last line ought save through record (a taggable). work if >:inverse_of set:
class taggable < activerecord::base belongs_to :post belongs_to :tag, :inverse_of => :taggings end
to me of seems inconsistent , highly confusing. in general, see no harm in saying inverse_of on every relationship. problem? i've seen lot of people ask on , haven't seen solid yes or no anyone.
there no detrient specifiying , allow rails optimise loading of objects can go , down active record model relationship chain in both directions without getting strange bugs changing value on 1 object doesnt change it's reference.
the problem setting doesnt work types said before , if fail silently you'll getting affore mentione bugs if go wrong way down method chain after changing something
this rails api
d = dungeon.first t = d.traps.first d.level == t.dungeon.level # => true d.level = 10 d.level == t.dungeon.level # => false
the dungeon instances d , t.dungeon in above example refer same object data database, different in-memory copies of data. specifying :inverse_of option on associations lets tell active record inverse relationships , optimise object loading.
Comments
Post a Comment