I have a module that adds logging by hooking into the after_save and
after_destroy callback queues.
module DbLog
def self.included(base)
RAILS_DEFAULT_LOGGER.info "DbLog included in #{base}"
RAILS_DEFAULT_LOGGER.info "Call stack"
caller.each { |e| RAILS_DEFAULT_LOGGER.info e }
base.after_destroy do |model|
...
end
base.after_save do |model|
...
end
end
end
The callbacks are being called twice in some cases causing me to get
duplicate log entries.
By looking at the call stack I found that the problem is the module is
being included twice due to associations.
class Person < ActiveRecord::Base
include DbLog
has_one :church_i_pastor, :class_name => "Church",
:foreign_key
=> "pastor_id"
end
class Church < ActiveRecord::Base
include DbLog
belongs_to :pastor, :class_name => "Person", :foreign_key
=>
"pastor_id"
end
The call stack shows DbLog included in Church, then DbLog included in
Person (from the association in Church), then DbLog included AGAIN in
Church from the association in Person. Am I approaching this the wrong
way or is there a bug here?
Thanks.
--
Jack Christensen
jackc-/SOt/BrQZzOj3I+7jmQ39gC/G2K4zDHf@public.gmane.org
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
Why not include it in ActiveRecord::Base? class ActiveRecord::Base include DbLog end My ruby-fu is still pretty weak, but it seems like that should work. -- rick http://techno-weenie.net
Rick Olson wrote:>Why not include it in ActiveRecord::Base? > >class ActiveRecord::Base > include DbLog >end > >My ruby-fu is still pretty weak, but it seems like that should work. > > > > > >I need it on some of my record types, but not all, so I need to include it on a model by model basis. -- Jack Christensen jackc-/SOt/BrQZzOj3I+7jmQ39gC/G2K4zDHf@public.gmane.org
add a class method which toggles a flag to true after its called. Example: class Pastor logs_events end On 4/22/05, Jack Christensen <jackc-/SOt/BrQZzMOf2zXYvRtkodd74u8MsAO@public.gmane.org> wrote:> Rick Olson wrote: > > >Why not include it in ActiveRecord::Base? > > > >class ActiveRecord::Base > > include DbLog > >end > > > >My ruby-fu is still pretty weak, but it seems like that should work. > > > > > > > > > > > > > I need it on some of my record types, but not all, so I need to include > it on a model by model basis. > > -- > Jack Christensen > jackc-/SOt/BrQZzOj3I+7jmQ39gC/G2K4zDHf@public.gmane.org > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Tobi http://www.snowdevil.ca - Snowboards that don''t suck http://www.hieraki.org - Open source book authoring http://blog.leetsoft.com - Technical weblog