[Nitro] The has_one/belongs_to relationship
Matthew B Gardner
weather at speakeasy.net
Sat Aug 18 17:36:13 EDT 2007
I think I'm working my way through it and it seems to be working correctly
itself, for the most part. I do have some questions...
> > p @character #=> nil
> > p @character_oid #=> 5
> > update(:only => [:character_oid]) # slight optimization #=> 07-08-18
> > 11:42:07 [ERROR] (Account) Account#update unknown message -
> > {:only=>[:character_oid]}
>
> totally unexplainable to me, I always use update(). George, any recent
> work from you on this (don't have a clean repo from you readily accessable
> here).
>
> > Despite the error message, it does setup its @character_oid in the
> > database, but you can see that @character is still nil. Also, the
> > corresponding Character isn't loaded and attached when the Account object
> > is loaded from the database -- my has_many/belongs_to relationship does
> > this (and rather beautifully, I might add) -- should I expect the same
> > from this relationship?
>
> Ahh... I see in the code that @character does not get set when applying,
> only when you call .character then. Though I think you're doing that.
> (since calling .character right after applying is kind of useless, as you
> already had the character (when putting it in), but ... might be kind of
> misleading performance wise that it pulls the object from db right after
> adding it.)
>
> So yes, it does the 'correct thing' here (debatable). Calling
> self.character right after that instead of @character must pull it from the
> db and everything works normally. That the object is saved in @character
> is more of a 'implementation detail' anyway...
>
Is Character#account_oid not saved until I call Account#update? I can't say
for sure, but I think the field was NULL in the db after Character#create,
but was then set in the db after Account#update. Just trying to clarify how
this works.
> Pulling one object from the db (the account in this case) does not preload
> the character. When calling account.character it must get it though.
> (The has_many doesn't preload either, that'd be crazy ^^;)
>
I may have misspoken -- what I meant is that the has_many objects were loaded
and attached when I loaded the object they belong to. The Character object is
now also loading and attaching when I load the corresponding Account object.
I'm assuming, from what you said, that this loading and attachment happens
during Og#allocate?
> So, please tell if self.character does not return the character, that'd be
> a major bug.
>
As far as I can tell, it's working -- no bug squashing needed on this front.
I'm still feeling this relationship out though -- I'm actually curious about
why I can refer to Character#account by "account", but Account#character only
by self.character and why it treats "character" as a local variable (but
not "account" in the Character class).
Thanks again for the help -- I'll keep you posted (pardon the pun) on any
further difficulties.
-Matt
More information about the Nitro-general
mailing list