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

谁都会走 提交于 2020-01-13 07:53:11

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/30962979/should-you-still-pass-only-the-object-id-when-using-activejob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!