Module: EdgeRider::PreloadAssociations

Defined in:
lib/edge_rider/preload_associations.rb

Defined Under Namespace

Modules: PreloadAssociationsInstanceMethod

Instance Method Summary collapse

Instance Method Details

#preload_associations(*args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/edge_rider/preload_associations.rb', line 9

def preload_associations(*args)
  preloader = ActiveRecord::Associations::Preloader

  if preloader.method_defined?(:run) # Rails 3.2 / Rails 4
    preloader.new(*args).run
  elsif preloader.method_defined?(:preload) # Rails 5 to Rails 6.1
    preloader.new.preload(*args)
  else # Rails 7+
    records = args.first
    associations = args.second
    options = args[2] || {}
    preloader.new(records: records, associations: associations, **options).call
  end
end