Assume a query of the following form
operatingExpenses = Expense.find(:all,
{:select=>"categories.activityType, categories.name heading, sum(amount) totalAmount",
:joins => "inner join expense_categories categories on category_id = categories.id ",
:group => "categories.activityType, categories.name",
:order => "categories.activityType, totalAmount DESC"}
)
Now amount is defined as a decimal field in the database schema. e.g. definition in the Rails Migration would be
create_table :expenses do |table|
table.column :created_at, :timestamp, :null=>false
table.column :description, :string, :limit=>100, :null=>false
table.column :amount, :decimal, :scale=>2, :precision=>10, :null=>false
table.column :category_id, :integer, {:null=>false, :default =>1}
end
Now the set of records returned by the query fails the following assert
assert_equal 800, operatingExpenses[1].totalAmount
<800> expected but was <"800.00">.
Why is the Sum/aggregate column returned as a string instead of the same datatype as the summed up database column? I'd like to avoid sprinkling .to_s
or .to_f
everywhere to get around this. Any ideas ?
What I'm trying to get at the end of this rainbow is a cashflow listing like this - (for a specified daterange.. an additional condition to the query listed above).
cat Type Heading TotalAmount
| Operating | ExpCatX001 | 4000 |
| | ExpCatX002 | 200 |
| Financing | ExpCatX003 | 1000 |
| Investing | ExpCatX004 | 4234 |
| | ExpCatX005 | 201 |