Для следующего кода RubyOnRails есть способ переместить вычисление "прибыли" из представления в модель ... так что, возможно, есть атрибут с именами total_income и total_expense * * 1005
Модель - транзакция.rb
class Transaction < ActiveRecord::Base
attr_accessible :name, :amount, :category
scope :incomes, :conditions => { :category => 'Income' }
scope :expenses, :conditions => { :category => 'Expense' }
end
Контроллер - Transactions_controller.rb
class TransactionsController < ApplicationController
def index
@incomes = Transaction.incomes
@expenses = Transaction.expenses
@transaction = Transaction.new
end
Просмотр - index.html.erb
<code><pre>
<strong>Income</strong>
<% @incomes.each do |income| %>
<%= income.name %> - <%= number_to_currency((income.amount.nil? ? 0 : income.amount)) %>
<% end %>
<strong>Subtotal:</strong> <%= number_to_currency(@income_total = @incomes.sum(:amount)) %>
<strong>Expenses</strong>
<% @expenses.each do |expense| %>
<%= expense.name %> - <%= number_to_currency((expense.amount.nil? ? 0 : expense.amount)) %>
<% end %>
<strong>Subtotal:</strong> <%= number_to_currency(@expenses_total = @expenses.sum(:amount)) %>
<strong>Profit: <%= number_to_currency(@income_total - @expenses_total) %></strong>