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