7

Github Opting out of strict loading on a per-record base by ayrton · Pull Reques...

 3 years ago
source link: https://github.com/rails/rails/pull/41181
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Contributor

Author

ayrton commented 25 days ago

edited

Would love to hear if we should ship with lazy loaded records, when serialization happens through globalid, to avoid having to write:

record.strict_loading!(false)

in your jobs:

module MyApplication
  class Application < Rails::Application
    config.active_record.strict_loading_by_default = true
  end
end

class Subscription < ApplicationRecord
  belongs_to :user
end

class User < ApplicationRecord
  has_many :subscriptions, inverse_of: :user
end

class ArchiveSubscriptionJob < ApplicationJob
  def perform(subscription)
    subscription.strict_loading!(false) # <-- should this be done out-of-the-box?

    user = subscription.user
    # [...]
  end
end

If so I am happy to draft up the code (either in this PR or a different one). Something along the lines of:

# activejob/lib/active_job/arguments.rb
def deserialize_global_id(hash)
  GlobalID::Locator.locate(hash[GLOBALID_KEY]).tap do |record|
    record.strict_loading!(false) if record.respond_to?(:strict_loading!)
  end
end

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK