使用 ActiveJob 时是否仍应仅传递对象 ID? [英] Should you still pass only the object id when using ActiveJob?

查看:49
本文介绍了使用 ActiveJob 时是否仍应仅传递对象 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ActiveJob 中执行以下操作的优缺点是什么:

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

选项 A:

// Controller
MyJob.perform_later(object.id)

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

选项 B:

// Controller
MyJob.perform_later(object)

// my_job.rb
def perform(object)
  // do stuff
end

推荐答案

ActiveJob 现在 在幕后使用新的 Globalid 来序列化/反序列化 ActiveRecord 实例,因此您现在可以传递 ActiveRecord 对象.

我个人更喜欢继续传递 ID,因为它使代码与其他组件更具互操作性,并且不会无限期地将我的代码与特定的 ActiveJob 行为联系起来,但这更像是个人选择.

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.

这篇关于使用 ActiveJob 时是否仍应仅传递对象 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆