Pass newly created object to after_create callback in Rails
Asked Answered
T

2

6

Everytime an object has been created i want to enqueue it in a Redis queue to check for certain properties. How can i add the created object directly as a parameter to the callback? So my redis job would do something like this:

class FurtherProcessCarJob
 #....

 def self.perform(order)
   puts order.id
 end 
end

whereas in the model

after_create Resque.enqueue FurtherProcessCar, #self

It is possible to hook a method to the callback and there look for the car again and the enqueue the object, but is it possible to do it directly?

Trews answered 6/11, 2018 at 9:19 Comment(1)
after_create { | record | Resque.enqueue record } should be working as well? see api.rubyonrails.org/classes/ActiveRecord/Callbacks.html first exampleIceboat
M
4

As I understand your question, something like this should work

class YourModel < ActiveRecord::Base
  #....
  after_create :enqueue_to_redis

  private 

  def enque_to_redis
     Resque.enqueue self, other_args
  end 
end
Minhminho answered 6/11, 2018 at 9:32 Comment(1)
Excellent! I tried smth similar, or maybe even that, but it was not working ;)Trews
H
0

Add in your model

class YourModel < ApplicationRecord

 after_commit do 
    YourModelJob.perform_later(self.id)
  end

end
Heiskell answered 1/9, 2022 at 19:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.