Forum: Ruby on Rails Rolled my own alternative to trigger function with OLD/NEW

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.
429500a5a54600958c9c7ac032a37f66?d=identicon&s=25 Joe (Guest)
on 2006-02-28 04:23
Back with PostgreSQL, I just inspected OLD and NEW in trigger functions.
I came up with this alternative in Rails:

class Item < ActiveRecord::Base

  def active=(value)
    self.set_old('active', self.active)

    write_attribute(:active, value)
  end

  def after_update
    if !get_old('active') and self.active
      Notifier::deliver_item_approved(self)
    end

    if get_old('active') != self.active
      Maintenance.delete_cache('...') # I wrote my own alternative to
expire_fragment that actually works in models. Grumble...
    end
  end

  protected

  def get_old(value)
    @old[value] unless @old.nil?
  end

  def set_old(key, value)
    (@old ||= {})[key] = value
  end

end

Feel free to expound on it! I tested it out and didn't get any nil
errors (I'm still shaky with nils - I guess get_old defaults to false if
@old[value] is nil?). I'm also clueless why making the get/set_old
functions to private causes an error.

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