rubocop/rubocop-rails

Enforce Passing Object IDs Instead of Objects to Background Job

Closed this issue · 2 comments

Feature Description.

It might be beneficial to have a cop in RuboCop Rails that enforces passing object IDs (e.g., user.id) instead of full objects (e.g., user) to background jobs such as Delayed::Job or ActiveJob. This practice would save memory efficiency and prevent potential serialization errors.

Rationale behind this suggestion:

  • Memory Inefficiency
  • Stale Data
  • Serialization Errors Reduction

Describe the solution you'd like

Add a new cop that checks for instances where a full object is passed to a job. The cop should recommend passing the object’s ID instead.

# bad MyJob.perform_later(user)

# good MyJob.perform_later(user.id) or other alternatives`

Additional context

Please let me know your thoughts on this issue, I am also willing to work on this issue if approved.

It's very hard to impossible for rubocop to actually know if a passed argument is an object or not. Your first example could just as well contain an integer in the user variable.

I believe your two last points aren't actually are an issue. Objects are roundtripped via something called globalid which in effect is just a string telling ActiveJob how to fetch it from the database again. https://github.com/rails/globalid. So it just does what you would do yourself, with a bit more overhead.

koic commented

I agree with @Earlopain opinion. So, I will close this proposal. Thank you.