Method: OccamsRecord::EagerLoaders::Builder#eager_load

Defined in:
lib/occams-record/eager_loaders/builder.rb

#eager_load(assoc, scope = nil, select: nil, use: nil, as: nil, from: nil, optimizer: :select, active_record_fallback: nil) { ... } ⇒ Object

Specify an association to be eager-loaded. For maximum memory savings, only SELECT the colums you actually need.

If you pass a block to nest more eager loads, you may call it with one of two forms: with an argument and without:

If you ommit the block argument, the “self” inside the block will be the eager loader. You can call “eager_load” and “scope” directly.

If you include the block argument, the “self” inside the block is the same as the self outside the block. The argument will be the eager loader, which you can use to make additional “eager_load” or “scope” calls.

ActiveRecord::Relation on which you may call all the normal query hethods (select, where, etc) as well as any scopes you’ve defined on the model.

Parameters:

  • assoc (Symbol)

    name of association

  • scope (Proc) (defaults to: nil)

    a scope to apply to the query (optional). It will be passed an

  • select (String) (defaults to: nil)

    a custom SELECT statement, minus the SELECT (optional)

  • use (Array<Module>) (defaults to: nil)

    optional Module to include in the result class (single or array)

  • as (Symbol) (defaults to: nil)

    Load the association usign a different attribute name

  • from (Symbol) (defaults to: nil)

    Opposite of ‘as`. `assoc` is the custom name and `from` is the name of association on the ActiveRecord model.

  • optimizer (Symbol) (defaults to: :select)

    Only used for ‘through` associations. Options are :none (load all intermediate records) | :select (load all intermediate records but only SELECT the necessary columns)

  • active_record_fallback (Symbol) (defaults to: nil)

    If passed, missing methods will be forwarded to an ActiveRecord instance. Options are :lazy (allow lazy loading in the AR record) or :strict (require strict loading)

Yields:

  • a block where you may perform eager loading on this association (optional)

Returns:

  • self



31
32
33
34
# File 'lib/occams-record/eager_loaders/builder.rb', line 31

def eager_load(assoc, scope = nil, select: nil, use: nil, as: nil, from: nil, optimizer: :select, active_record_fallback: nil, &builder)
  @eager_loaders.add(assoc, scope, select: select, use: use, as: as, from: from, optimizer: optimizer, active_record_fallback: active_record_fallback, &builder)
  self
end