# ActiveRecord after save new and old values

**URL:** <https://www.ruby-forum.com/t/activerecord-after-save-new-and-old-values/60917>\
**Category:** Rails\
**Created:** [May 18, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/activerecord-after-save-new-and-old-values/60917 "2006-05-18T16:05:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ndorphine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@ndorphine](https://www.ruby-forum.com/u/ndorphine)\
**Post date:** [May 18, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/activerecord-after-save-new-and-old-values/60917/1 "2006-05-18T16:05:42Z")

</div>

I want to write a function that after an ActiveRecord saves  
successfully, can show me what the before and after values of a  
particular column are. Is this possible without creating a separate  
object before modifying the current one?

-John

---

<div class="post-metadata">

**Author:** ![ndorphine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@ndorphine](https://www.ruby-forum.com/u/ndorphine)\
**Post date:** [May 18, 2006, 7:11pm UTC](https://www.ruby-forum.com/t/activerecord-after-save-new-and-old-values/60917/2 "2006-05-18T19:11:11Z")

</div>

Hi John,

Have a look at callback methods and observer classes. The following is  
an example of using callback methods. Have a look at the modules  
ActiveRecord::Callbacks and ActiveRecord::Observer on  
[http://api.rubyonrails.org/](http://api.rubyonrails.org/) for a full description of what is possible

class Foo  
before\_save :collect\_before\_values  
after\_save : collect\_after\_values

def collect\_before\_values  
# …  
end

def collect\_after\_values  
# …  
end  
end

Hope that helps!

cheers  
Martin

---

<div class="post-metadata">

**Author:** ![ndorphine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@ndorphine](https://www.ruby-forum.com/u/ndorphine)\
**Post date:** [May 18, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/activerecord-after-save-new-and-old-values/60917/3 "2006-05-18T19:14:24Z")

</div>

Doh, of course the class definition should look like

class Foo \< ActiveRecord::Base

# …

end

cheers  
Martin

---

<div class="post-metadata">

**Author:** ![ndorphine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@ndorphine](https://www.ruby-forum.com/u/ndorphine)\
**Post date:** [May 24, 2006, 8:58pm UTC](https://www.ruby-forum.com/t/activerecord-after-save-new-and-old-values/60917/4 "2006-05-24T20:58:38Z")

</div>

Thanks,

That’s exactly what I wanted.

-John

On Thu, May 18, 2006 at 07:10:30PM +0200, Martin G. wrote:

> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
John O.  
Systems Administrator  
Yo!Africa  
E-Mail: john at [yoafrica.com](http://yoafrica.com)  
Tel: +263 4 858404  
echo ‘9k[l:l;s\<s=0l\>x]s"[1+l\<dd_l=d_-l;+ds\<rl=2\*\*l:+ds=d_rd_+4\<-d15\>\>]  
s\>[q]s-[d77/3_2-s;47l"x-P1+d78\>`]s`0[d23/.5-3_s:0l`xr10P1+d24\>$]ds$x’|dc
