On 6/25/05, Joshua <joshua-q0Yur7km1XpBDgjK7y7TUQ@public.gmane.org>
wrote:> Is there some way to observe when an association is added in a habtm
> relationship?
I was able to achieve this, but I had to create a method on one of my
models.  It basically called some custom methods in the observer.  It
went something like this:
# in the model...
def Auction.add_bid(bid)
  self.class.changed
  self.class.notify_observers(:before_add_bid, self, bid)
  self.bids << bid
end
# in the observer...
class AuctionObserver < ActiveRecord::Observer
  # needed to override ActiveRecord::Observer#update
  def new_update(callback_method, object, bid = nil)
    if bid
      send(callback_method, object, bid) if respond_to?(callback_method)
    else 
      old_update(callback_method, object)
    end
  end
  alias_method :old_update, :update
  alias_method :update, :new_update
  def before_add_bid(record, bid)
  end
end
Obviously, you can''t use the simple @model.items << @item syntax
anymore, but for me it was an ok tradeoff.  If anyone has a better
idea, let me know!
-- 
rick
http://techno-weenie.net