alireza nobakht
alireza nobakht

Reputation: 209

Rails 7 - ActiveRecord::Associations::Preloader.new.preload

consider this code:

# in Rails 6.1
def preload(resource, relations)
  ActiveRecord::Associations::Preloader.new.preload(resource, relations)
end

So: I want to change that for compatibility with Rails 7 so I wrote this:

def preload(resource, relations)
  ActiveRecord::Associations::Preloader.new(records: resource, associations: relations)
end

Did I do a right thing? because .preload(resource, relations) is not exit in Rails 7 anymore. if you have any other suggestion I'm so looking forward for it

Upvotes: 13

Views: 5096

Answers (1)

Ruslan Valeev
Ruslan Valeev

Reputation: 1907

You're almost there. It looks like this works:

    ActiveRecord::Associations::Preloader.new(
      records: [resource].flatten, # in case if resource is a single ApplicationRecord object
      associations: relations
    ).call

Upvotes: 18

Related Questions