Before_update/after_update debate

It seems as though there is no way to get the value of an attribute in
before_update, BEFORE the update occurs.

What exactly does before_update mean then? Is before_save called before
before_update? Are they different?

I’ve tried querying the database like:

def before_update
tran = Tran.find( self.id )
oldName = tran.Name
end

But it never returns the old value of Name.

Hi Blake,

On 2/18/07, Blake M. [email protected] wrote:

It seems as though there is no way to get the value of an attribute in
before_update, BEFORE the update occurs.

What exactly does before_update mean then? Is before_save called before
before_update? Are they different?

before_save is called before a save or an update, before_update is
only called before an update (e.g., #update_attributes).

I’ve tried querying the database like:

def before_update
tran = Tran.find( self.id )
oldName = tran.Name
end

But it never returns the old value of Name.

Hmm, seems to work for me.

Do you know if the hook is being run? If so, is it finding the old
record? What’s the value of tran?

If not, how are you doing the update? Might a validation be failing
preventing the update?

Regards,
George.

George O. wrote:

Hi Blake,

On 2/18/07, Blake M. [email protected] wrote:

It seems as though there is no way to get the value of an attribute in
before_update, BEFORE the update occurs.

What exactly does before_update mean then? Is before_save called before
before_update? Are they different?

before_save is called before a save or an update, before_update is
only called before an update (e.g., #update_attributes).

I’ve tried querying the database like:

def before_update
tran = Tran.find( self.id )
oldName = tran.Name
end

But it never returns the old value of Name.

Hmm, seems to work for me.

Do you know if the hook is being run? If so, is it finding the old
record? What’s the value of tran?

If not, how are you doing the update? Might a validation be failing
preventing the update?

Regards,
George.

Thanks George for the hints. You got me thinking that maybe my
before_update function wasn’t being called at all. But since that
didn’t seem too likely (cause I’ve got the function declaration just as
is in the Agile book), I knew that the problem had to be inside the
function.

This was my function:
def before_update

tran = Tran.find( self.id )

if Util.new.this_month_up_to_today?( tran.PaidDate )
  old_amount = tran.Amount
else
  old_amount = nil
end

old_account_id = tran.account.id

end

and in my after update, I was doing calculations on the old_amount
variable, which is actually a function defined like:

def old_amount=(amount)
@old_amount = amount
end

The problem stemmed from the fact that I wasn’t doing:
self.old_amount = tran.Amount

in my before_update function.

Thanks for the help!