【发布时间】:2011-11-28 00:59:53
【问题描述】:
我正在尝试通过 mixin 注入 after_save 回调,但我的 rspec 测试告诉我,当调用 create 方法时,回调被调用了两次。为什么方法会被调用两次?
以下 rspec 测试失败
it 'should call callback' do
Product.any_instance.should_receive(:update_linkable_attachments).once
Product.create(:name=>'abc')
end
失败信息是:
Failure/Error: Unable to find matching line from backtrace
(#<Product:0xb7db738>).update_linkable_attachments(any args)
expected: 1 time
received: 2 times
这是代码
module MainModuleSupport
def self.included(base)
base.instance_eval("after_save :update_linkable_attachments")
end
def update_linkable_attachments
LinkedAttachment.delay.create_from_attachment self
end
end
class Product < ActiveRecord::Base
include MainModuleSupport
...
end
Product 类有其他代码,但没有任何其他回调。
【问题讨论】:
-
Rails 版本?我认为这在几个版本中存在问题。
-
LinkedAttachment 可能会在创建时触发此问题,如果它与产品以某种方式相关
-
我创建了一个干净的 Rails 应用程序 3.0.10。它在那里工作正常。你能告诉我
LinkedAttachment.delay.create_from_attachment是什么吗?
标签: ruby-on-rails ruby-on-rails-3 activerecord