5

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

Jayesh Babu
  • 1,389
  • 2
  • 20
  • 34

1 Answers1

6

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
Ruslan Valeev
  • 1,529
  • 13
  • 24