Forum: Ruby on Rails after_update: old, new 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.
37641533d39c169de08d16c246b0f56a?d=identicon&s=25 Jack Davis (Guest)
on 2006-02-22 03:05
With after triggers in Postgres, there are special NEW and OLD vars
accessible to trigger functions. Any such thing in Rails? Or would it
entail doing something like setting a class var in before_update?

Jack
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 Francois Beausoleil (Guest)
on 2006-02-22 04:16
(Received via mailing list)
Hello Jack,

2006/2/21, Jack Davis <jack@davis.org>:
> With after triggers in Postgres, there are special NEW and OLD vars
> accessible to trigger functions. Any such thing in Rails? Or would it
> entail doing something like setting a class var in before_update?

Take a look at this:
http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tec...

In short, Rails doesn't do it for you.  Good thing is it's possible to
do so.

Bye !
37641533d39c169de08d16c246b0f56a?d=identicon&s=25 Jack Davis (Guest)
on 2006-02-22 04:43
Hmm, that's not how I want to go about it. I want to put it in the
model's before_ and/or after_update callback, where I think it belongs.
I've tried this:

class Item < ActiveRecord::Base
  def after_update
    logger.debug 'after_update'
    logger.debug "  old: #{@old.active}"
    logger.debug "  self: #{self.active}"
    if @old.active != self.active
      logger.debug 'active not equal'
    end

    @old = nil
  end

  def before_update
    logger.debug 'before_update'
    logger.debug "  active: #{self.active}"
    @old = self
    logger.debug "  old: #{@old.active}"
  end
end

class AdminController < ApplicationController
  def toggle_item
    @item = Item.find(params[:id])

    @item.update_attributes({:active=>!@item.active})
  end
end

But it doesn't work; the old and self values are always the same. I've
seen a lot of debate on this list about application vs. integration
databases and I think if Rails thinks it can replace triggers, et al in
the database it needs to offer equivalent OLD/NEW vars. From what I can
tell, the old values aren't available at all in the before/after_update
callbacks. Maybe I can do a separate find(self.id) in one of those
callbacks and then compare values. Yuck.

Jack
37641533d39c169de08d16c246b0f56a?d=identicon&s=25 Jack Davis (Guest)
on 2006-02-24 03:24
RailsWeenie:

This doesn't work:

def amount=(new_amount)
  (@old ||= {})[:amount] = self.amount
  self.amount = new_amount
end

http://rails.techno-weenie.net/question/2005/12/19...

I get a 'stack too deep' error (I'd guess self.amount is getting called
recursively). I replaced the second line with 'write_attribute :amount,
new_amount', but that doesn't appear to work for methods like 'toggle!'.

Jack
This topic is locked and can not be replied to.