Forum: Ruby on Rails When are activerecord associations made?

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.
Christopher Lee (Guest)
on 2009-03-20 22:34
(Received via mailing list)

My question is when ActiveRecord associations are actually made?  To
me, it doesn't seem like the relationship is made until of the objects
is saved into the database.  Let's say i have models like:

Library has_many :books
Book belongs_to :Library

If I have code such as:

book =

library =
library.books << book

it seems like library.books is empty UNTIL book is first saved.

this causes strange problems for me if I am trying to validate that a
book always belongs to a library, and a library has to have a least
one book.

Maybe I'm thinking about this the wrong way?

Thanks in advance,
Sijo k. (Guest)
on 2009-03-23 10:56
Write as

library =
book =

This topic is locked and can not be replied to.