Forum: Ruby on Rails ActiveRecord after save new and old values

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John O. (Guest)
on 2006-05-18 18:05
(Received via mailing list)
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
Martin G. (Guest)
on 2006-05-18 21:11
(Received via mailing list)
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/ 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
Martin G. (Guest)
on 2006-05-18 21:14
(Received via mailing list)
Doh, of course the class definition should look like

class Foo < ActiveRecord::Base
  # ...
end

cheers
Martin
John O. (Guest)
on 2006-05-24 22:58
(Received via mailing list)
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
> >

> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails


--
John O.
Systems Administrator
Yo!Africa
E-Mail:  john at 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
This topic is locked and can not be replied to.