# After\_create callback called twice in test env when using fi

**URL:** <https://www.ruby-forum.com/t/after-create-callback-called-twice-in-test-env-when-using-fi/113858>\
**Category:** Rails\
**Created:** [September 5, 2007, 5:58pm UTC](https://www.ruby-forum.com/t/after-create-callback-called-twice-in-test-env-when-using-fi/113858 "2007-09-05T17:58:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tsaleh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/cc9497/32.png) [@tsaleh](https://www.ruby-forum.com/u/tsaleh)\
**Post date:** [September 5, 2007, 5:58pm UTC](https://www.ruby-forum.com/t/after-create-callback-called-twice-in-test-env-when-using-fi/113858/1 "2007-09-05T17:58:22Z")

</div>

All,

I’ve been trying to figure out a strange bug in one of my applications  
that I think I’ve narrowed down to a problem with rails fixtures. It  
seems as though the after\_create callback is being run twice when I  
save a record. This is only happening a) in the test environment, and  
b) when there is a fixture file for that table.

The test below only prints “Doing stuff” one time if I remove the  
‘fixtures’ line. Likewise, saving the item through ./script/console  
in development mode also does not call the do\_stuff method twice.

I’m inclined to think that this is a bug in the handling of fixtures,  
but I’d love to be proven wrong. I couldn’t find this in trac, but if  
there’s an open ticket, then I’d like to know about it. If not, and  
if everyone agrees that this is a likely bug, then I’ll open one.

The model:

class Item \< ActiveRecord::Base  
after\_create :do\_stuff  
def do\_stuff  
puts “Doing stuff”  
end  
end

The test:

require File.dirname( **FILE** ) + ‘/…/test\_helper’  
require ‘item’

class ItemTest \< Test::Unit::TestCase  
fixtures :items

```
def test_callback
  @item = Item.new
  puts "========================"
  @item.save
  puts "========================"
end

```

end

The fixture file:

one:  
id: 1  
two:  
id: 2

The rake run:

# rake test:units

# (in /Users/tsaleh/Documents/Programming/svn/bug/trunk) /opt/local/bin/ruby -Ilib:test “/opt/local/lib/ruby/gems/1.8/gems/ rake-0.7.3/lib/rake/rake\_test\_loader.rb” “test/unit/item\_test.rb” Loaded suite /opt/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake/ rake\_test\_loader Started

# Doing stuff Doing stuff

.  
Finished in 0.051473 seconds.

1 tests, 0 assertions, 0 failures, 0 errors

## Thanks for the help, Tammer

> **[Tammer Saleh](http://tammersaleh.com)**
>
> Tammer's home online.

removed\_email\_address@domain.invalid  
(626) 841-0708  
Usually online via Google Talk

---

<div class="post-metadata">

**Author:** ![tsaleh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/cc9497/32.png) [@tsaleh](https://www.ruby-forum.com/u/tsaleh)\
**Post date:** [June 14, 2008, 12:25am UTC](https://www.ruby-forum.com/t/after-create-callback-called-twice-in-test-env-when-using-fi/113858/2 "2008-06-14T00:25:36Z")

</div>

Tammer Saleh wrote:

> All,
> 
> I’ve been trying to figure out a strange bug in one of my applications  
> that I think I’ve narrowed down to a problem with rails fixtures. It

Try this in your environment.rb:

module ActiveRecord  
module Callbacks  
def callbacks\_for(method)  
(self.class.read\_inheritable\_attribute(method.to\_sym) or []).uniq  
end  
end  
end

- OR -

Use the alternate form of defining a callback:

def after\_create

# …

end

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

end

---

<div class="post-metadata">

**Author:** ![tsaleh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/cc9497/32.png) [@tsaleh](https://www.ruby-forum.com/u/tsaleh)\
**Post date:** [June 14, 2008, 12:30am UTC](https://www.ruby-forum.com/t/after-create-callback-called-twice-in-test-env-when-using-fi/113858/3 "2008-06-14T00:30:24Z")

</div>

Brian H. wrote:

> Tammer Saleh wrote:
> 
> > All,
> > 
> > I’ve been trying to figure out a strange bug in one of my applications

Or, perhaps more cleanly:

module Callbacks  
def callbacks\_for\_with\_uniq(method)  
callbacks\_for\_without\_uniq(method).uniq  
end

alias\_method\_chain :callbacks\_for, :uniq  
end
