[Nitro] The has_one/belongs_to relationship

Matthew B Gardner weather at speakeasy.net
Sat Aug 18 18:03:45 EDT 2007

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).

I'm assuming I'm doing this to myself, here is how I'm initializing Character:

class Account
	has_one :character, Character

	def foo
		self.character = Character.create msg.capitalize, self
	      	update(:only => [:character_oid]) # slight optimization

class Character
	belongs_to :account, Account

	def initialize name, acc
		self.account = acc

Let me know if that isn't enough information, or if it's unclear.

Thanks for any help (getting closer!),

More information about the Nitro-general mailing list