inverse_of not working as documented

Please take a look at this gist http://gist.github.com/395267 .

It seems inverse_of is not working as I expect it to. Or am I missing
something here?

Thanks

It seems inverse_of is not working as I expect it to. Or am I missing
something here?

inverse_of isn't an identity map, it only handles traversing
associations. So in your case:

dungeon.traps.map {|t| t.dungeon.object_id }

All those object instances will be the same.

Thanks Michael for the clarification.

Any thoughts on should Trap.first.dungeon be blowing up?

- Neeraj

Any thoughts on should Trap.first.dungeon be blowing up?

Yes, you want inverse_of=>:traps, not :trap.

Should inverse_of work with scopes? For example, the following loads
different dungeons:

dungeon.traps.order('id desc').map { |t| t.dungeon.object_id }

Should inverse_of work with scopes? For example, the following loads
different dungeons:

dungeon.traps.order('id desc').map { |t| t.dungeon.object_id }

The current implementation only does that for the instances on the
associations themselves, not for values returned by finders.

If you wanted to investigate making that change you could give it a
go, but it's probably a fairly intrusive change and not really
suitable for inclusion until 3.1. Of course, who knows, maybe it'd be
easy :slight_smile:

It looks feasible to pass along a new :inverse option to
NamedScope::scope, and further along as a "@inverse_value" variable to
Relation. It would need to carry both the association @owner and the
name of the owner attribute. In Relation#to_a, it would finally be
used on the loaded records.

Another option is to add an :after_load callback, which gets called by
Relation#to_a for each loaded record. Seems a bit heavyweight right
now, but separates the responsibilities.

Thinking out loud won't get us anywhere. I'll try tinkering with it in
the meantime.