I have a model that's using activerecord lifecycle callbacks pretty heavily. I'm using the after_commit callback to execute sidekiq jobs that require a primary key to run, on create.

after_commit on: :create do
  async_process
end

The code inside the block is never run.

However, when I do

after_commit :on => :create do
  async_process
end

The code runs fine.

As I understand it, these two different lines should be interpreted exactly the same way. What am I missing?

I'm using ruby 2.0.0p247, Rails 3.2.17.

有帮助吗?

解决方案

It works for me using same Ruby and Rails versions. Alternatively, you can try with another accepted syntax:

after_commit :async_process, on: :create

其他提示

I've found using the aliases (rather than :on solved this issue for me:

after_create_commit { CreateS3BucketWorker.perform_async(id) }

All of my logic is already extracted to a sidekiq worker so creating a named function just to call the worker seemed excessive. Passing in a block let's me call the worker and keep my callback to 1 line.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top