On Sun, Feb 19, 2006 at 10:09:37AM -0800, martin wrote:
} I would like to create a model that is based on two database tables:
} id (PK)
} id (PK)
} There is a one-to-one relationship between the records of Table1 and
} Table2. (That is, for every record in Table1 there is a record with
} matching id in Table2 and vice versa).
} The problem is that I cannot alter the table schemas in any way, so I
} cannot add tableX_id columns to create foreign key relationships.
} What would be the proper implementation of this problem?
Pick one to be the “primary” table and the other to be the “dependent”
table. Use has_one with an explicit column name for the “foreign” key.
the model class you can add extra accessors that give the dependent
fields, so it looks like a single record. I think there’s another
to has_one that says that it should load the row from the dependent
up front, rather than lazily waiting for an access, which may or may not
If you’re feeling extra clever you can even override method_missing to
create accessors for dependent fields rather than explicitly writing
but that’s probably unnecessary.
} If it makes matters easier, I only require read access.
Doesn’t matter, except that you only need accessors, not mutators.
} Thank you in advance for the help!