Forum: Ruby on Rails Pre-populating a has_many relationship?

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.
A24e589de07cb769a71f28aead957a48?d=identicon&s=25 Duane Morin (dmorin)
on 2007-07-24 18:10
I am disappointed with myself that I can't find the answer to this, but
I've spent the morning trying various things and am obviously missing
something.  I have a simple setup where a Plan has_many Years.  What I
want to do, upon creation of a new Plan, is to also set up a certain
number of Year objects.  Each Year will be initialized according to
variables dependent on context, and the number of them will also vary
depending on the situation.  But as a rule, each Plan will start out
life with more than one Year, so I'd like to do that right in the
constructor and be done with it.

What is the syntax for how I would access years inside of Plan?  I found
write_attribute(:foo, 'bar') for dealing with traditional attributes,
but it didn't seem totally relevant to what I want to do here.

I'm drawing a complete blank.  Outside the actual Plan object, it works

a =
a.years <<>'works')
a.years <<>'also works')

I just want that to work inside the Plan constructor.  What am I

This topic is locked and can not be replied to.