title: Rails Creation through association & Collection vs. Array
body: <p>I am confused about the some Association concepts in Active Records.</p>
  
  <p>I have three models User, Bank and Bankaccount. Both the User and the Bank models "has_many" Bankaccounts and the Bankaccount model "belongs_to" both the User and the Bank models. I use the following syntax to create a Bankaccount through its association with User</p>
  
  <pre><code>@bankaccount = @user.bankaccounts.create(bankaccount_params)
  </code></pre>
  
  <p>What is the appropriate syntax if I want to create a bankaccount object through both the association with User and the association with Bank?</p>
  
  <p>My second question is related to this one. Right now, because I am not sure how to create a bankaccount through both associations, I handle the association with the Bank by putting the parameter manually</p>
  
  <pre><code>  bank_id = params[:bank_id]
  </code></pre>
  
  <p>However, this seems to trigger some issues down the road when I want to iterate through all the bankaccounts and retrieve the name of the associated bank.</p>
  
  <p>In my view I have</p>
  
  <pre><code>&lt;% @bankaccounts.each do |bankaccount| %&gt;
  &lt;%= bankaccount.bank %&gt;
  </code></pre>
  
  <p>I obtained a list of these</p>
  
  <pre><code>#&lt;Bank:0x007f7a66618ef0&gt;
  #&lt;Bank:0x007f7a664c9ab8&gt;
  </code></pre>
  
  <p>If I tried to get the name of the bank</p>
  
  <pre><code>&lt;% @bankaccounts.each do |bankaccount| %&gt;
  &lt;%= bankaccount.bank.name %&gt;
  </code></pre>
  
  <p>I get an undefined method name for nil class. I do get the name of the bank in the console with these simple lines</p>
  
  <pre><code>bankaccount = Bankaccount.find(1)
  bankaccount.bank.name
  </code></pre>
  
  <p>Could you anyone give me more background on those concepts and provide me with the appropriate syntax to loop accross my collection @user.bankaccount and for each bankaccount retrieve the name of the associated bank?</p>
  
  <p>Thanks.</p>