# Before\_update/after\_update debate

**URL:** <https://www.ruby-forum.com/t/before-update-after-update-debate/85960>\
**Category:** Rails\
**Created:** [February 18, 2007, 7:40am UTC](https://www.ruby-forum.com/t/before-update-after-update-debate/85960 "2007-02-18T07:40:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![blakeage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b38774/32.png) [@blakeage](https://www.ruby-forum.com/u/blakeage)\
**Post date:** [February 18, 2007, 7:40am UTC](https://www.ruby-forum.com/t/before-update-after-update-debate/85960/1 "2007-02-18T07:40:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![blakeage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b38774/32.png) [@blakeage](https://www.ruby-forum.com/u/blakeage)\
**Post date:** [February 18, 2007, 4:27pm UTC](https://www.ruby-forum.com/t/before-update-after-update-debate/85960/2 "2007-02-18T16:27:40Z")

</div>

Hi Blake,

On 2/18/07, Blake M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![blakeage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b38774/32.png) [@blakeage](https://www.ruby-forum.com/u/blakeage)\
**Post date:** [February 19, 2007, 3:57am UTC](https://www.ruby-forum.com/t/before-update-after-update-debate/85960/3 "2007-02-19T03:57:44Z")

</div>

George O. wrote:

> Hi Blake,
> 
> On 2/18/07, Blake M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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!
