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 -- Posted via http://www.ruby-forum.com/.
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/Tech/Ruby/LoggingChanged.rdoc In short, Rails doesn''t do it for you. Good thing is it''s possible to do so. Bye ! -- Fran?ois Beausoleil http://blog.teksol.info/
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
--
Posted via http://www.ruby-forum.com/.
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/how_to_find_out_about_changed_activerecord_attributes
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
--
Posted via http://www.ruby-forum.com/.