Marc Bungart
Marc Bungart

Reputation: 31

Rails - model update decimal - render rounded value from database

I have a decimal field called label_length in my model Offer with one decimal place. The value gets rounded when saved into the database.

A record is already stored in the database. I’m in the edit form for my Offer model. Now I put 123.45 into the text field and hit the button. After saving I render the edit action again with the text field. In the text field for label_length I find the value 123.45 again although 123.5 is stored in the database.

It seems that @offer in the controller is not updated after calling offer.update_attributes(params[:offer])

Is there a way to display the current database value?

Thanks Marc

Upvotes: 1

Views: 842

Answers (1)

coreyward
coreyward

Reputation: 80041

What does your migration look like? You can prompt Rails to create a decimal field with a set scale and precision in your migration so that you don't have values unexpectedly rounded:

t.decimal :amount,  :precision => 6, :scale => 2

As far as getting Rails to use the data from the database after saving, you can simply add in a call to reload like so:

if @offer.save
  @offer.reload
  ...
else
  ...
end

Upvotes: 1

Related Questions