Hello,
I used a migration to add an integer column to one of my data tables:
add_column :lien, :cpc, :integer, :null => false
The user is not interested in this integer, but in 1/1 000 000 of it.
So, in my form partial I used a text_field as usual:
<%= text_field ‘lien’, ‘cpc’ %>
But in my controller, I modify the cpc value to get an integer:
def edit_lien
@lien = Lien.find(params[:id])
@lien.cpc /= 1000000
end
def update_lien
@lien = Lien.find(params[:id])
@lien.update_attributes(params[:lien])
@lien.cpc *= 1000000
@lien.save
end
This works great as long as the user enters an integer value in the
text field (if ‘3’ is entered, the database contains 3000000 and the
user still sees ‘3’ when re-editing). But decimal values are truncated
(if ‘1.5’ is entered, the database contains 1000000 instead of 1500000).
I guess that’s because I defined my cpc column as integer in the first
place, can I tell Rails to not truncate decimal values as I will use
them, or should I have gone another way to make this work properly?
Thanks,
Karine