Forum: Ruby on Rails Circular Relationships

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
8bf22a4f6ef6257becd26cdd6b0b687c?d=identicon&s=25 Trevor Johns (Guest)
on 2007-01-09 03:57
(Received via mailing list)
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.


Trevor Johns
This topic is locked and can not be replied to.