How to stop callbacks from getting called twice

Maybe this is old news, but it took me all afternoon:

In my test environment, an 'after_create' callback was getting called twice unexpectedly. In my model, I had a simple 'after_create :foo' declaration.

From what I understand, the problem seems to be that the 'after_create :foo, :bar' syntax appends those symbols to an array without doing a 'uniq'. It seems that if you happen to load the class twice, you'll get double entries in the callback list. This can happen easily because the Ruby 'require' method doesn't seem to convert paths to absolute paths, e.g the following will cause the file '/lib/foo.rb' to be loaded twice:

# In \app\models\bar.rb require File.join(RAILS_ROOT, 'lib', 'foo') require '../../lib/foo' class Bar < ActiveRecord::Base


So, I added this to my environment.rb:

module Callbacks   def callbacks_for_with_uniq(method)     callbacks_for_without_uniq(method).uniq   end

  alias_method_chain :callbacks_for, :uniq end

- OR -

You could use the alternate form of defining a callback, but this is less convenient.

def after_create   # ... end