Circular Relationships

Hello,
I have a (hopefully) quick question about working in Rails, and I was
hoping somebody would be able to lend a hand. I’m trying to create an
app and it has two models, let’s call them ‘A’ and ‘B’.

The problem I’m having is that A includes a reference to B, and B
includes a reference to A. As a result, if neither exist than both need

to be created at the exact same time. However, when I call save() on an

instance of A, Rails doesn’t seem to update that instance with it’s ID
in the database, making it very hard to set B’s reference to A.

Further, even if I had the ID, I have to do at least three DB
transactions: Create A, Create B (with reference to A), Update A (with
reference to B).

So, my question is twofold:

  1. Is there some way to find what ID an ActiveRecord object is given by

the DB when I call save()?
2. Is there some way to do this more efficiently.

Thanks.


Trevor Johns

On Jan 7, 2007, at 8:33 PM, Trevor Johns wrote:

So, my question is twofold:

  1. Is there some way to find what ID an ActiveRecord object is
    given by the DB when I call save()?

Wrap the entire thing in a transaction.

  1. Is there some way to do this more efficiently.

A.foos << B
A.save

-faisal