# Pre-populating a has\_many relationship?

**URL:** <https://www.ruby-forum.com/t/pre-populating-a-has-many-relationship/110253>\
**Category:** Rails\
**Created:** [July 24, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/pre-populating-a-has-many-relationship/110253 "2007-07-24T18:10:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [July 24, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/pre-populating-a-has-many-relationship/110253/1 "2007-07-24T18:10:03Z")

</div>

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  
fine:

a = Plan.new  
a.years \<\< Year.new(:comment=\>‘works’)  
a.years \<\< Year.new(:comment=\>‘also works’)

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

Thanks!
