Sha256: 7b547544da9430ea2d50a1c60c68db2248fc1c85f43fbc397d283de82e38a335

Contents?: true

Size: 880 Bytes

Versions: 10

Compression:

Stored size: 880 Bytes

Contents

# frozen_string_literal: true

require "n1_loader/ar_lazy_preload"

require_relative 'context/setup_ar_lazy'
require_relative 'context/setup_database'

class Loader < N1Loader::Loader
  def perform(users)
    total_per_user = Payment.group(:user_id).where(user: users).sum(:amount).tap { |h| h.default = 0 }

    users.each do |user|
      total = total_per_user[user.id]
      fulfill(user, total)
    end
  end
end

class User < ActiveRecord::Base
  has_many :payments
end

class Payment < ActiveRecord::Base
  belongs_to :user

  validates :amount, presence: true
end

fill_database

# Has N+1 and loads redundant data
p User.all.map { |user| user.payments.sum(&:amount) }

# Has no N+1 and loads only required data
p User.preload_associations_lazily.all.map { |user| Loader.for(user) }

# or
ArLazyPreload.config.auto_preload = true
p User.all.map { |user| Loader.for(user) }

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
n1_loader-1.7.2 examples/ar_lazy_integration_with_isolated_loader.rb
n1_loader-1.7.1 examples/ar_lazy_integration_with_isolated_loader.rb
n1_loader-1.7.0 examples/ar_lazy_integration_with_isolated_loader.rb
n1_loader-1.6.6 examples/ar_lazy_integration_with_isolated_loader.rb
n1_loader-1.6.5 examples/ar_lazy_integration_with_isolated_loader.rb
n1_loader-1.6.4 examples/ar_lazy_integration_with_isolated_loader.rb
n1_loader-1.6.3 examples/ar_lazy_integration_with_isolated_loader.rb
n1_loader-1.6.2 examples/ar_lazy_integration_with_isolated_loader.rb
n1_loader-1.6.1 examples/ar_lazy_integration_with_isolated_loader.rb
n1_loader-1.6.0 examples/ar_lazy_integration_with_isolated_loader.rb