Module: Uberloader::QueryMethods::InstanceMethods

Defined in:
lib/uberloader/query_methods.rb

Instance Method Summary collapse

Instance Method Details

#preload_associations(records) ⇒ Object

Overrides preload_associations in ActiveRecord::Relation



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/uberloader/query_methods.rb', line 54

def preload_associations(records)
  if (uberloads = @values[:uberloads])
    preload = preload_values
    preload += includes_values unless eager_loading?
    uberloads.add_preload_values preload

    strict = respond_to?(:strict_loading_value) ? strict_loading_value : nil
    uberloads.uberload! records, strict
  else
    super
  end
end

#uberload(association, scope: nil) {|Uberloader::Context| ... } ⇒ ActiveRecord::Relation

Uberload an association.

Category.all.
  uberload(:widget, scope: Widget.order(:name)) { |u|
    u.uberload(:parts) {
      u.scope Part.active
      u.uberload(:foo)
    }
  }

Parameters:

  • association (Symbol)

    Name of the association

  • scope (ActiveRecord::Relation) (defaults to: nil)

    Optional scope to apply to the association’s query

Yields:

Returns:

  • (ActiveRecord::Relation)


42
43
44
# File 'lib/uberloader/query_methods.rb', line 42

def uberload(association, scope: nil, &block)
  spawn.uberload!(association, scope: scope, &block)
end

#uberload!(association, scope: nil, &block) ⇒ Object

See uberload



47
48
49
50
51
# File 'lib/uberloader/query_methods.rb', line 47

def uberload!(association, scope: nil, &block)
  @values[:uberloads] ||= Collection.new(Context.new)
  @values[:uberloads].add(association, scope: scope, &block)
  self
end