多个after_commit用于相同的方法

时间:2013-08-19 12:04:23

标签: ruby-on-rails-3 callback

如何指定方法的多个回调?

after_commit :assign_awards, on: [:create, :destroy]

private

def assign_awards
  AwardsWorker.perform_async(self.id, self.class.name, self.user_id)
end

错误:

/activesupport-3.2.13/lib/active_support/callbacks.rb:404: syntax error, unexpected '[', expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END
...(transaction_include_action?(:[:create, :destroy]))

由于某种原因,回调不会执行此操作:

after_commit :assign_awards, on: :create
after_commit :assign_awards, on: :destroy

只有在我使用两种不同的方法时才有效:

after_commit :assign_awards_create, on: :create
after_commit :assign_awards_destroy, on: :destroy

private

def assign_awards_create
  AwardsWorker.perform_async(self.id, self.class.name, self.user_id)
end

def assign_awards_destroy
  AwardsWorker.perform_async(self.id, self.class.name, self.user_id)
end

1 个答案:

答案 0 :(得分:0)

嗯,这个答案显示了相反的结果:
https://stackoverflow.com/a/10356903/1023609

可能是您正在使用的Rails版本存在问题。