I have a form with a text_field tag for phone numbers, which I would like to save the information to the database as a pure integer. This I though would involve a before_save that would essentially remove all the (), - etc.
I found in a different post that something like this could overwrite the value that comes in from the field "phone".
def original_number=(value)
value.gsub!(/\D/, '') if num.is_a?(String)
write_attribute(:original_number, num.to_i)
end
But, the =(value) is confusing me a bit. How would I write that as a before_save parameter?
i.e before_save #######
def #######
end
How would I write that for original_number=(value) ?
Here's what my model looks like so far
# == Schema Information
#
# Table name: phones
#
# id :integer not null, primary key
# name :string(255)
# twilio_number :integer
# original_number :integer
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class Phone < ActiveRecord::Base
attr_accessible :original_number, :user_id, :name, :twilio_number
belongs_to :user
validates :name, presence: true
validates :twilio_number, presence: true
validates :original_number, presence: true
validates :user_id, presence: true
default_scope order: 'phones.created_at DESC'
end