Reputation: 3466
I have a model class as shown below:
class Product < ActiveRecord::Base
belongs_to :user;
attr_accessible :price
before_save :check_if_price_changed
after_save :notify_about_price_change
def check_if_price_changed
if (price.changed?) then
@price_changed = true
else
@price_changed = false
end
end
....
I want to record if the price of the product changed before I save it to the database. Then I have a routine that will do the notification once the product has been persisted successfully with the new price. But I get the following error when I try to check if the price attribute is dirty:
undefined method `changed?' for 20:Fixnum
Is that method not supported out of the box in rails 3.1? Am I calling it incorrectly or in the incorrect layer (model vs controller)?
Upvotes: 3
Views: 3782
Reputation: 4499
This will return a boolean reflecting whether or not the price value has changed.
def check_if_price_changed
self.price_changed?
end
Have a look at the ActiveModel::Dirty documentation.
Upvotes: 4