FactoryGirlで create
した場合、 after_commit
だけ実行されない。
class Book < ActiveRecord::Base
before_create do
puts 'before_create'
end
after_create do
puts 'after_create'
end
after_commit :run_something, on: :create
private
def run_something
puts 'after_commit'
end
end
FactoryGirl.define do
factory :book
end
FactoryGirl.create(:book)
# => before_create
# => after_create
実行したい場合はcallbackを自分で実行しないといけない。例えば以下のようにすればいい。
FactoryGirl.define do
factory :book do
after(:create) do |book|
book.__send__(:run_something)
end
end
end
FactoryGirl.create(:book)
# => before_create
# => after_create
# => after_commit