Rolled my own alternative to trigger function with OLD/NEW


#1

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