How to use update_attributes with custom tuning methods in Rails?

I have a model called Contact with the phone_number attribute. I have some customization methods like

 def prefix=(num) self.phone_number[3..5] = num end 

When I call @contact.update_attributes({:prefix => '510'}) , I get no errors and @contact gets the changes, but the changes do not get into the database. I tried using attr_accessible to specifically allow the use of these setter methods to no avail.

Is there a way to make update_attributes work for me? I am using Rails 2.3.8.

+6
ruby-on-rails activerecord attributes
source share
1 answer

You must tell the rails that you changed the attribute when making changes in place:

 def prefix=(num) phone_number_will_change! self.phone_number[3..5] = num end 
+13
source share

All Articles