theDrifter
theDrifter

Reputation: 1706

Pass newly created object to after_create callback in Rails

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?

Upvotes: 6

Views: 2085

Answers (2)

sparkle
sparkle

Reputation: 7398

Add in your model

class YourModel < ApplicationRecord

 after_commit do 
    YourModelJob.perform_later(self.id)
  end

end

Upvotes: 0

VAD
VAD

Reputation: 2401

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

Upvotes: 4

Related Questions