As outlined here:
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
inverse_of appears to tell Rails to Cache the in memory associations and minimize Database Queries. Their example is:
class Dungeon < ActiveRecord::Base
has_many :traps, :inverse_of => :dungeon
has_one :evil_wizard, :inverse_of => :dungeon
end
class Trap < ActiveRecord::Base
belongs_to :dungeon, :inverse_of => :traps
end
Which they immediatly follow with:
for `belongs_to` associations `has_many` inverse associations are ignored.
So I have several questions.
- Are inverse associations ignored on
has_many
for abelongs_to
? If so, how does their example make sense? Shouldn't it just not do anything? As far as I can tell (assuming it does anything) All this allows to do is something like:
dungeon.traps.first.dungeon
with the final call to
.dungeon
NOT generating an entire new query, but merely reaching for the in memory association. Assuming that is correct, why would I ever NOT want that behavior? Why wouldn't I just stickinverse_of:
on every association?