Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changed? call on properties not working

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)?

like image 682
Tabrez Avatar asked Dec 05 '25 13:12

Tabrez


1 Answers

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.

like image 148
miked Avatar answered Dec 07 '25 10:12

miked



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!