ActiveRecord field normalization
Posted
by Bill
on Stack Overflow
See other posts from Stack Overflow
or by Bill
Published on 2009-05-19T16:44:53Z
Indexed on
2010/05/11
20:04 UTC
Read the original article
Hit count: 267
I feel bad asking this question, as I thought I knew enough about Activerecord to answer this myslef. But such is the way of having SO available ...
I'm trying to remove the commas from a field in a model of mine, I want the user to be able to type a number , ie 10,000 and that number be stored in the database as 10000. I was hoping that I could do some model-side normalization to remove the comma. I don't want to depend on the view or controller to properly format my data.
I tried ;
before_validation :normalize
def normalize
self['thenumber'] = self['thenumber'].to_s.gsub(',','')
end
no worky :(
© Stack Overflow or respective owner