Yes, you should be able to enqueue a delayed_job task from an ActiveRecord callback. To install and use delayed_job:
- Add
gem 'delayed_job_active_record'
to yourGemfile
and runbundle install
. Create the delayed_job support tables in your database by running:
rails generate delayed_job:active_record
rake db:migrate
In your model:
class MyModel < ActiveRecord::Base
after_commit :get_geolocation, on: :create
private
def get_geolocation
end
handle_asynchronously :get_geolocation
end
Notice that you should use after_commit
instead of after_create
to schedule your job, so you avoid situations where the job executes before the transaction is committed.