dimanche 18 octobre 2015

When performing a migration in Ruby on Rails to make a decimal column default 0, it becomes negative in the schema. What am I doing wrong?

I was following the steps described here: http://ift.tt/1Ks0JTT

For a little background, this web app is for an auction management system. The amount_raised column in the Auction class is supposed to represent a dollar amount, the amount of money that is made at the auction. By default, I wanted to have it be $0.00. My set method in the auction.rb file is as follows:

def amount_raised=(val)
  currency = val.to_s.gsub(/[-$,]/,'').to_f if val.present?
  write_attribute(:amount_raised, currency)
end

The first migration I tried to make is the following:

change_column_default :auctions, :amount_raised, from: nil, to: 0.00

But, this change resulted in the following in my schema.db file (precision and scale were already there):

t.decimal  "amount_raised", precision: 5, scale: 2, default: -0.0

I tried to make another migration to change the default from -0.0 to nil (because I figured if I couldn't get 0 to work I could do the proper format in the view by checking for a nil and having it print $0.00) but the default remained at -0.0.

I have attempted to rollback the migrations but they are throwing exceptions as being "irreversible migrations" which is what I was trying to avoid by using from: nil, to: 0.00 to begin with.

My questions are, having followed the format from the Rails guide, what did I do incorrectly? What is the best way to fix this?

Aucun commentaire:

Enregistrer un commentaire