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
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
Hello Jack,
2006/2/21, Jack D. [email protected]:
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 !
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=>[email protected]})
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
RailsWeenie:
This doesn’t work:
def amount=(new_amount)
(@old ||= {})[:amount] = self.amount
self.amount = new_amount
end
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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs