I have searched and it looks like this should be simple but I can't get it to work. I am trying to remove all non-numeric characters prior to saving a phone number. This is what I have currently:
before_save { |workorder| workorder.phonenumber =
phonenumber.to_s.gsub(/D/, '').to_i }
So if a user inputs
925-555-5555
It should save 9255555555 but it is actually saving only 925 and ignoring everything after
I also tried:
before_save { |workorder| workorder.phonenumber =
phonenumber.to_s.gsub(/[^0-9]/, "").to_i }
with the same result.
Solved:
def raw_phonenumber
self.phonenumber
end
def raw_phonenumber=(s)
self.phonenumber=s.gsub(/D/, '')
end
See Question&Answers more detail:os