I wrote an app in Rails and created a query to count the last_insurance
and sum by type_money
but when I show it in my text_field_tag
I'm getting another value:
#######this error is inside my text_field_tag
#<Policy:0x7feed11412d0>
Here is the description of the query:
http://sqlfiddle.com/#!2/72f5c/1
Here are my models:
class Insurance < ActiveRecord::Base
belongs_to :policy
end
class Policy < ActiveRecord::Base
unloadable
has_many :insurances
end
This is my controller:
class PolicyController < ApplicationController
def generate_print_calculator
@dolar = Policy.find_by_sql("SELECT sum(i1.net_insurance) total
FROM (
SELECT max(id) id FROM insurances
GROUP BY policy_id
) i2
JOIN insurances i1 USING (id)
JOIN policies p ON p.id = i1.policy_id
WHERE p.type_money = 1
GROUP BY p.type_money")
end
end
This is my view:
Suma Dólars :
<%= text_field_tag "dolar", @dolar %>
When I tried this in MYSQL I got:
|total|
426913.49999999977
And, when I show @dolar
in my view I got this inside my textfield:
#<Policy:0x7feed11412d0>
@dolar
is not showing in my view and I'm getting another value. Also, when I refresh it I'm getting another strange value:
#<Policy:0x7feed0e05440>
Here is an example what I tried with less policies and is working:
http://sqlfiddle.com/#!2/72f5c/1
Can somebody help me with this?
You need to do <%= text_field_tag "dolar", @dolar.first.total %>
in your view.
@dolar.first.total
looks better then @dolar[0].total
...just a visual thing no change in output