title: Creating multiple records on one form in Rails app
body: <p>I've been struggling on this for the last couple of day and some help would be greatly appreciated. A Rails app that has two models:</p>
  
  <ul>
  <li>Accounts - name[string], credit[boolean], active[boolean]</li>
  <li>Balances - balance[decimal], date[date], account_id[integer]</li>
  </ul>
  
  <p>When creating a new <code>balance</code> at <code>http://localhost:3000/balances/new</code>, the form looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/pOjnN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pOjnN.png" alt="enter image description here"></a></p>
  
  <p>How can I enable users to be able to create multiple balances at the same time?  There should only be one textbox for the date field which should be used for all the balances records created but there should be multiple account dropdown lists and balance textboxes on a form.</p>
  
  <p>I've tried looking up nested forms but I'm struggling.</p>
  
  <p><strong>CODE</strong></p>
  
  <p>schema</p>
  
  <pre><code>  create_table "accounts", force: :cascade do |t|
      t.string   "name"
      t.boolean  "credit"
      t.boolean  "active"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
    end
  
    create_table "balances", force: :cascade do |t|
      t.decimal  "balance"
      t.date     "date"
      t.integer  "account_id"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
    end
  </code></pre>
  
  <p>account.rb</p>
  
  <pre><code>class Account &lt; ActiveRecord::Base
    has_many :balances
  
    validates :name, presence: true, length: { maximum: 250 },
                    uniqueness: { case_sensitive: false }
  end
  </code></pre>
  
  <p>balance.rb</p>
  
  <pre><code>class Balance &lt; ActiveRecord::Base
    belongs_to :account
    #default_scope -&gt; { order(date: :desc) }
  
    validates :account, presence: true, length: { maximum: 250 }
  end
  </code></pre>
  
  <p>accounts_controller.rb</p>
  
  <pre><code>....
  def new
    @account = Account.new
  end
  
  def create
    @account = Account.new(account_params)
  
    respond_to do |format|
      if @account.save
        format.html { redirect_to accounts_path, notice: 'Account was successfully created.' }
        format.json { render :show, status: :created, location: @account }
      else
        format.html { render :new }
        format.json { render json: @account.errors, status: :unprocessable_entity }
      end
    end
  end
  ....
  </code></pre>
  
  <p>balances_controller.rb</p>
  
  <pre><code>....
  def new
    @balance = Balance.new
  end
  
  def create
    @balance = Balance.new(balance_params)
  
    respond_to do |format|
      if @balance.save
        format.html { redirect_to balances_path, notice: 'Balance was successfully created.' }
        format.json { render :show, status: :created, location: @balance }
      else
        format.html { render :new }
        format.json { render json: @balance.errors, status: :unprocessable_entity }
      end
    end
  end
  ....
  </code></pre>
  
  <p>balances/_form.html.erb</p>
  
  <pre><code>&lt;%= form_for(@balance) do |f| %&gt;
    &lt;% if @balance.errors.any? %&gt;
      &lt;div id="error_explanation"&gt;
        &lt;h2&gt;&lt;%= pluralize(@balance.errors.count, "error") %&gt; prohibited this balance from being saved:&lt;/h2&gt;
  
        &lt;ul&gt;
        &lt;% @balance.errors.full_messages.each do |message| %&gt;
          &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt;
        &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  
    &lt;div class="field"&gt;
      &lt;%= f.label :date %&gt;&lt;br&gt;
      &lt;%= f.text_field :date %&gt;
    &lt;/div&gt;
  
    &lt;div class="field"&gt;
      &lt;%= f.label :account_id %&gt;&lt;br&gt;
      &lt;%= f.collection_select(:account_id, Account.all.where(active: true).order('name ASC'), :id, :name,{})%&gt;
    &lt;/div&gt;
  
    &lt;div class="field"&gt;
      &lt;%= f.label :balance %&gt;&lt;br&gt;
      &lt;%= f.text_field :balance %&gt;
    &lt;/div&gt;
  
    &lt;div class="actions"&gt;
      &lt;%= f.submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>