Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 400
alireza nobakht Avatar asked Feb 23 '26 03:02

alireza nobakht


1 Answers

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
like image 150
Ruslan Valeev Avatar answered Feb 27 '26 02:02

Ruslan Valeev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!