title: How to pass foreign key attributes down through a nested form in Rails 3
body: <p>I have three models:  </p>
  
  <pre><code>class Client &lt; ActiveRecord::Base
     has_many :balancesheets
     has_many :investment_assets, :through =&gt; :balancesheets
  
  class Balancesheet &lt; ActiveRecord::Base
     belongs_to :client
     has_many :investment_assets, :dependent =&gt; :destroy
     accepts_nested_attributes_for :investment_assets, :allow_destroy =&gt; true
  
  class InvestmentAsset &lt; ActiveRecord::Base
      belongs_to :balancesheet
         belongs_to :client
  </code></pre>
  
  <p>I have two questions that have to do with the foreign key client_id. First, when I create a new balancesheet, I use collection_select to select the client from the list. I would rather put the new balancesheet link on the client show page and just pass the client id through to the form so I don't have to choose a client from a list or type it in a field. So first, how do I do that?</p>
  
  <p>Second, the investment_asset model is nested in the balancesheet form. Everything works just fine, except the client_id attribute for the investment_asset is blank. Im not sure why because my associations seem to be okay. So the question is, how do I pass this client_id attribute down through the nested form? I can post models or controllers, just let me know.</p>
  
  <p><strong>UPDATE</strong> I have since found the answer to my first question <a href="https://stackoverflow.com/questions/5719499/how-to-pass-object-id">here</a>. However, I am still trying to figure out the second part based on that answer, which is, how do I pass this client_id down through a nested form? To show how it worked passing user id when creating a balancesheet, here is the client show page link:  </p>
  
  <pre><code>&lt;%= link_to 'New BalanceSheet',new_balancesheet_path(:id =&gt; @client.id) %&gt;  
  </code></pre>
  
  <p>In the balancesheet form I have a hidden field:  </p>
  
  <pre><code> &lt;%= f.hidden_field :client_id %&gt;  
  </code></pre>
  
  <p>And in the Balancesheet Controller this is the new action:  </p>
  
  <pre><code> @balancesheet = Balancesheet.new(:client_id =&gt; params[:id])
  </code></pre>
  
  <p>This works just fine. So for an investment_asset I am using <a href="https://github.com/ryanb/nested_form" rel="nofollow noreferrer">Ryan Bates nested_form gem</a> which has a little bit different syntax for a link. So inside the balancesheet form the link to add a new investment_asset is:  </p>
  
  <pre><code> &lt;%= f.link_to_add "Add asset", :investment_assets %&gt;
  </code></pre>
  
  <p>I can't figure out how to pass the user id like I did on the balancesheet with:  </p>
  
  <pre><code>(:id =&gt; @client.id)
  </code></pre>
  
  <p>I can put the same thing in a new action in the investment_asset controller and add a hidden field, but I'm not sure how to pass it in on the link. Thoughts?</p>