[Nitro] The has_one/belongs_to relationship
Matthew B Gardner
weather at speakeasy.net
Sun Aug 19 16:57:32 EDT 2007
>On Sunday 19 August 2007 14:42, Jonathan Buch wrote:
> > I may have spoken a little prematurely on this -- I am encountering a
> > problem. When I call Character#create, or when a Character object is
> > loaded (via it's corresponding Account being loaded), it's creating it's
> > own account. To illustrate, this is what's happening:
> > a1 = Account.new
> > a1.character = Character.create self
> > a1.equal?(a1.character.account) #=> false
> > The same thing is happening when loaded from the database, so the account
> > is pointing to the character, but the character is pointing to a
> > different account (an account with the same attributes, but a different
> > identity).
> Yes, a different 'identity', but not 'equality', depending on which method
> is called. ;)
> Ruby knows .eql? .equal? .== .===
> .equal? is for testing if the object ids are the same, which they are not
> in this case.
Well, my issue is that I need my Account object to reference my Character
object and my Character object to refer back to that same Account object. I
assumed that the relationship would work like this:
Account <-> Character
But, it's doing this, I think:
Account -> Character -> Account (different instance)
Is this how it works then, or did I perhaps implement something incorrectly?
Since it's just a one_on_one relationship, I can always reference the Account
myself in the Character class, but I was trying to be Ogish and have it load
and attach itself. However, it's essential that the created objects reference
eachother -- do I need to do the loading myself then
(Character.find_by_account_oid(Account#oid), where Character#account_oid is a
field created and set by me)?
> Og overrides ==() to react on the primary key of that model.
Oh, this probably explains some unexpected results from Array#delete(self) in
my Character class -- I assumed it was Og-related, but good to know for sure
More information about the Nitro-general