In my rails project I have a form to enter a dollar amount. This amount is stored as an integer in a mysql database.
<%=f.label :Amount%><br/>
<%=f.text_field :amount%>
if the user enters the number using a “.” separator like 2034.34 it works well. My problem is when someone uses a “,” as in 2,034. This number is stored just as a 2 in the databases. How do I get the app to store number with both comma and decimal separators?
Update
Nkm put on the right track but I got a stack too deep error. I ended up using
def amount=(amt)
write_attribute(:amount,amt.gsub(",", ""))
end
Since the database column is
decimal/float, we should process the data into the right format before saving.You can either go with
activerecordbefore_savecallback or override the setter method of that attribute as follows,