How to format values before saving to database in rails 3
activerecord, ruby, ruby-on-rails, ruby-on-rails-3
Solution
Try this:
def profit=(new_profit)
self[:profit] = new_profit.gsub(/[^0-9]/, '')
end
Problem
I have a User model with Profit field. Profit field is a DECIMAL (11,0) type. I have a masked input on the form which allows user to input something like $1,000. I want to format that value and remove everything except numbers from it so i will have 1000 saved. Here is what i have so far: ``` class User < ActiveRecord::Base before_save :format_values private def format_values self.profit.to_s.delete!('^0-9') unless self.profit.nil? end end ``` But it keeps saving 0 in database. Looks like it is converting it to decimal before my formatting function.