It seems when I grab some hierarchical ActiveRecord structure that there are quite a few hits to the database. I improved this using the :include option to flesh out as much of the structure as possible. Even so, it seems that ActiveRecord does not map the reciprocal sides of a relationship (e.g. parent-child) with unique references to a record. That is, we get a navigable ActiveRecord structure, but one that to my knowledge doesn’t guarantee a unique copy of a given record.
node = Node.find(1, :include => {:document => :node})
node.object_id #A
node.document.node.object_id #B although I expect/desire A
Unavoidable in ActiveRecord? Have others moved to alternative ORMs because of shortcomings like this? (Don’t get me wrong: ActiveRecord is a great tool. But all tools have strengths and weaknesses.)
Say I were to write my own SQL query (it could even be a stored proc) returning multiple result sets (one per table) containing all the relevant data for my ActiveRecord hierarchy. Is there a painless way that I can explicitly map the associations without having ActiveRecord mistaking my explicit mappings for an attempt to create new associations?
I simply don’t think ActiveRecord is designed to support this idea. It simply means you have to work with ActiveRecord, understanding its pros and cons. I’ve since found ways to work around this issue.
I’ve looked at DataMapper and I see that it does specifially address this issue: