Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should you still pass only the object id when using ActiveJob?

What are the pros and cons of doing the following in ActiveJob:

Option A:

# Controller
MyJob.perform_later(object.id)

# my_job.rb
def perform(object_id)
  object = Object.find(object_id)
  # do stuff
end

Option B:

# Controller
MyJob.perform_later(object)

# my_job.rb
def perform(object)
  # do stuff
end
like image 407
Jacob Avatar asked Jun 21 '15 09:06

Jacob


Video Answer


1 Answers

ActiveJob now uses the new Globalid library behind the scenes to serialize/deserialize an ActiveRecord instance, therefore you can now pass an ActiveRecord object.

I personally prefer to keep passing the ID as it makes the code more interoperable with other components and it doesn't tie my code indefinitely to a specific ActiveJob behavior, but this is more a personal choice.

like image 114
Simone Carletti Avatar answered Oct 16 '22 13:10

Simone Carletti