[Nitro] The has_one/belongs_to relationship

Jonathan Buch john at oxyliquit.de
Sun Aug 19 14:42:10 EDT 2007


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

Ah, no, nothing gets 'autosaved' just by setting something (would be quite
inefficient, saving 10 values in a row would mean 10 queries, unacceptable).
By doing a .save or .update you then send changes to the DB.

> 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?

Ah yes, 'loaded and attached', that'd be a good description on what it does.  :)

On the internals... well, that always depends on how the object is loaded.

`MyModel[oid]` for example would only make a SELECT and hand it over to
`read_one()`  (sql.rb) which then creates a new object (MyModel.allocate, which
only, if you didn't know that, prevents ruby from calling the `initialize()`).
The newly allocated object then is filled by `read_row()`, which just sets
instance variables (one for each field) on that object.
Conceptually pretty simple I think.  :)

> As far as I can tell, it's working -- no bug squashing needed on this front.

Good good.  :)

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

Well... simple Ruby:

class A
   attr_accessor :a

   def set(o)
     a = o # this will set an instance variable
     self.a = o # this will use `def a=()` which gets created by attr_acc

A.new.set 'foo'
A.new.a = 'foo' # will do the same

The same applies for both Character and Account, not sure how your exact code
looks like of course.

Hope that helps,


Feel the love

More information about the Nitro-general mailing list