[Nitro] The has_one/belongs_to relationship

Jonathan Buch john at oxyliquit.de
Sat Aug 18 16:19:14 EDT 2007


Hi,

>> self.character = Character.create()
>> p @character, @character_oid
>> # save!
>> update(:only => [:character_oid]) # slight optimization
>
> 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...

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 ^^;)

So, please tell if self.character does not return the character, that'd be a
major bug.

> Thank you for any assistance you can offer,

I allways hope to be of help.  :)

Jo

-- 
Feel the love
http://pinkjuice.com/pics/ruby.png


More information about the Nitro-general mailing list