Bhav
Bhav

Reputation: 2207

Populate drop down list with data and use selected item in query in Rails

I've got two basic Models:

- Account [id, name]
- Balance [id, account_id, date, balance]

Their controllers contain the default code from scaffolding plus I've modified the balances controller with a 'statement' function. This allows me to calculate the total balances of all the accounts at the most recent date stored in the balances table.

This is currently fine and working as expected, and my statement view displays the data.

However, how can I display a drop down list of all the 'dates' from the 'balances' table. And then set the @latestDate in the balances_controller.rb to the selected date in the drop down list.

I've tried adding this in statement.html.erb:

  <%= form_for(@balance) do |b| %>
    <%= b.label :date %><br>
    <%= b.collection_select(:date, @dates, :id, :date) %>
  <% end %>

However, I'm not sure if this is the correct approach and this throws the following error:

Couldn't find Balance with 'id'=

MY CODE:

account.rb

class Account < ActiveRecord::Base
  has_many :balances
  validates :name, presence: true, length: { maximum: 250 },
                  uniqueness: { case_sensitive: false }
end

balance.rb

class Balance < ActiveRecord::Base
  belongs_to :account
  validates :account, presence: true, length: { maximum: 250 }
end

balances_controller.rb

  def index
    @balances = Balance.all.order(date: :desc)
  end

  def new
    @balance = Balance.new
  end

  def statement
    @dates = Balance.all.order('date desc').dates
    @latestDate = Balance.order('date desc').first.date

    @summaryBalances = Balance.joins(:account).order('accounts.credit').where('date = :abc', {abc: @latestDate})

    @assetTotal = Balance.joins(:account).where('date = :abc', {abc: @latestDate}).where("credit = 'f'").sum(:balance)
    @creditTotal = Balance.joins(:account).where('date = :abc', {abc: @latestDate}).where("credit = 't'").sum(:balance)

    @worth = @assetTotal - @creditTotal
  end

balances/statement.html.erb

<h1>Statement at <%= @latestDate %></h1>

<p>(showing all balances from the (select top 1 date from balances))</p>

<div class="table-responsive">
<table class="table">
  <thead>
    <tr>
      <th>Account</th>
      <th>Balance</th>
      <th colspan="1"></th>
    </tr>
  </thead>

  <tbody>
    <% @summaryBalances.each do |balance| %>
      <tr>
          <% if !(balance.account.credit) %>
            <td><%= balance.account.name %></td>
          <% else %>
            <td><%= balance.account.name + ' (Credit)' %></td>
          <% end %>

        <td class='account-balance'><%= number_to_currency(balance.balance, unit: "£") %></td>
        <td><%= link_to 'Edit', edit_balance_path(balance) %></td>
      </tr>
    <% end %>
  </tbody>
</table>
</div>

<h5>Assets:</h5>
<%= number_to_currency(@assetTotal, unit: "£") %>
<h5>Liabilities:</h5>
<%= number_to_currency(@creditTotal, unit: "£") %>
<h5>Net Worth:</h5>
<%= number_to_currency(@worth, unit: "£") %>

Upvotes: 1

Views: 875

Answers (1)

Michael Cruz
Michael Cruz

Reputation: 882

Based on your comments, simply add :statement to that before_action as follows:

before_action :set_balance, only: [:show, :edit, :update, :destroy, :statement]

Since you reference @balances from the form as well, you'll need to set this within the statement action on the controller.

def statement
  @balances = Balance.all # Or whichever balances you want
  # Your existing code here...
end

Upvotes: 1

Related Questions