[Nitro] The has_one/belongs_to relationship

Jonathan Buch john at oxyliquit.de
Mon Aug 20 20:07:11 EDT 2007


>> Thanks so much for the help -- I do have one more question, when you get
>> the time. I have a Node class that has_many comments (which belong_to a
>> Node) -- when I load the Node table, it loads the nodes and loads and
>> attaches the comments (which I still find really cool, btw) -- that's all
>> that's happening there, correct?

>> What I mean is, the comments aren't also
>> loading their respective Nodes, as the Character was loading its Account,
>> are they? Just wanted to double check that I don't have a bunch of extra
>> Node instances running around.

Yes, that's all there is to it.  When you don't call .node you won't have
extra node copies around.

> Just ran a test and it looks like they are running around (or maybe they're
> running around b/c the test included referencing comment->node). At any rate,
> it looks like relationships may not be a great idea for my project.

class Node
   has_many Comment
class Comment
   belongs_to Node

n = Node.new
n.comments << Comment.create
n.comments << Comment.create

c1 = n.comments.first
c2 = n.comments.last

n # original node
c1.node # node copy #2
c2.node # node copy #3

You now have 3 copies of the original node lying about.  Unecessarily.

Relationships _are_ a good idea, think about normalizing of databases.  :)

It's really just about being a little careful on where the objects come from,
use available local or instance variables to store object you are reusing,
don't do unnecessary methods calls (which .node is).

What Og can't do is update every relationship object and set the referral to
the parent object to the identical (object_id) object.

Hope that helps,


Feel the love

More information about the Nitro-general mailing list