title: Create association before record is saved
body: <p>So, I'm admittedly a Rails newbie, and I'm running into what must be a fairly common issue, but I can't find the answer here.</p>
  
  <p>I have a model <code>Foo</code> like this:</p>
  
  <pre><code>class Foo &lt; ActiveRecord::Base
    has_many :bars
  end
  </code></pre>
  
  <p>Bars belongs_to Foo, all that works.  Now I want to create a Foo and build Bar at the same time.  Like this:</p>
  
  <pre><code>f = Foo.new(:baz =&gt; 'baz')
  bars.each do |b|
    f.bars.build(:bizzy =&gt; b[:bizzy])
  end
  f.save
  </code></pre>
  
  <p>I know this won't work because the parent record doesn't exist, so the association doesn't exist, but there must be a way to do this.  I've temporarily gotten around it by editing to this:</p>
  
  <pre><code>f = Foo.new(:baz =&gt; 'baz')
  f.save
  f = Foo.find(:first, :conditions =&gt; {:baz =&gt; 'baz'})
  bars.each do |b|
    f.bars.create(:bizzy =&gt; b[:bizzy])
  end
  </code></pre>
  
  <p>But that is not clean, and is all around unpleasant.</p>
  
  <p>What is the right way to do this?</p>