【问题标题】:Rails 7 - ActiveRecord::Associations::Preloader.new.preloadRails 7 - ActiveRecord::Associations::Preloader.new.preload
【发布时间】:2022-12-27 20:48:56
【问题描述】:

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

【问题讨论】:

    标签: ruby-on-rails-6 ruby-on-rails-7


    【解决方案1】:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-11
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多