views:

66

answers:

3

I have a model that has an amount and I'm tracking to see if this amount is changed with a Model.amount_changed? with a before_save which works fine but when I check to see amount_was and amount_change? it only returns the updated amount not the previous amount. And all this is happening before it is save. It knows when the attribute is changed but it will not return the old value.

Ideas?

class Reservation < ActiveRecord::Base

before_save  :status_amount, :if => :status_amount_changed

def status_amount_changed
  if self.amount_changed? && !self.new_record?
    true
  else
    false
  end
end

def status_amount
    title = "Changed Amount"
    description = "to #{self.amount_was} changed to #{self.amount} units"
    create_reservation_event(title, description)
end

def create_reservation_event(title, description)
    Event.create(:reservation => self, :sharedorder => self.sharedorder, :title => title,     :description => description, :retailer => self.retailer )
end

end
+1  A: 

Rails provide "Dirty Objects" to keep track if something changes in your model. For instance, say your model has a name attribute:

my_model = MyModel.find(:first)
my_model.changed?  # it returns false

# You can Track changes to attributes with my_model.name_changed? accessor
my_model.name  # returns  "Name"
my_model.name = "New Name"
my_model.name_changed? # returns true

# Access previous value with name_was accessor
my_model.name_was  # "Name"

# You can also see both the previous and the current values, using name_change
my_model.name_change  #=> ["Name", "New Name"]

So if you want to store the old value in your db, you can use two attributes in you model (say amount and amount_was) saving them during the update_attributes takes place. Otherwise, if you don't need the amount_was history, you can just use two instance variables.

If you need somthing more, like tracking your model history, Rails has a nice dedicated plugin. You can find all the info via Ryan Bates RailsCast #177

microspino
amount is the attribute, _was is the method to find the before changed value. Same method. I saw the railscasts.
Sam
A: 

Hi Sam

You can create another vertual attribute in your module

attr_accessor :initial_amount

and when you are loading data from DB, fill the initial_amount with amount

then you can check

(initial_amount == amount ? true : false)

cheers, sameera

sameera207
how to get the initial value
Sam
A: 

Use *_was call to get the old value:

p m.amount_was if m.amount_changed?
KandadaBoggu