Does anyone have any sample code for creating custom callbacks for observers? I don''t believe there''s a callback that is called when adding or removing items in a has_and_belongs_to_many association. I''d like to kick off an email when this happens (to notify a bidder that they''ve been outbid, for instance). If you don''t have some code just laying around, I''ll take another stab at it later today perhaps. Thanks -- rick http://techno-weenie.net
On Wednesday 01 June 2005 05:14, Rick Olson wrote:> Does anyone have any sample code for creating custom callbacks for > observers?I''ve written following small extension for AR::Base, to be able to notify observers by hand. class ActiveRecord::Base def self.custom_notify(model, method) return unless model model.go_notify(method) end def go_notify(method) self.send(:notify, method) self end end I''m using it in one of my projects to track when user was authorized. def self.authenticate(login, pass) user = find_first(...) custom_notify(user, "authenticated") end #custom_notify is for use in class scope, and #go_notify is for instance scope. I''m not sure about what''s the best way to connect this to habtm though. HTH, -- sdmitry -=- Dmitry V. Sabanin http://sabanin.ru
On 6/2/05, Dmitry V. Sabanin <sdmitry-okj3p8EEQC8@public.gmane.org> wrote:> On Wednesday 01 June 2005 05:14, Rick Olson wrote: > > Does anyone have any sample code for creating custom callbacks for > > observers? > I''ve written following small extension for AR::Base, to be able to > notify observers by hand.I went a different route with my own example. Part of the reason I did this is that my callback needed access to both models in a has_and_belongs_to_many association. I posted the code on Snippets: http://www.bigbold.com/snippets/posts/show/361 -- rick http://techno-weenie.net