Method: OccamsRecord::EagerLoaders::Builder#eager_load_many

Defined in:

#eager_load_many(name, mapping, sql, binds: {}, model: nil, use: nil) { ... } ⇒ Object

Specify some arbitrary SQL to be loaded into some arbitrary attribute (“name”). The attribute will hold an array of 0 or more associated records.

In the example below, :parts is NOT an association on Widget. Though if it where it would be a has_many. The mapping argument says “The id column in the parent (Widget) maps to the widget_id column in the children.

The %ids bind param will be provided for you, and in this case will be all the id values from the Widget query.

res = OccamsRecord
  .eager_load_many(:parts, {:id => :widget_id}, "
    SELECT * FROM parts WHERE widget_id IN (%{ids}) AND sku NOT IN (%{bad_skus})
  ", binds: {
    bad_skus: ["G90023ASDf0"]


  • name (Symbol)

    name of attribute to load records into

  • mapping (Hash)

    a Hash that defines the key mapping of the parent ( to the children (parts.widget_id).

  • sql (String)

    the SQL to query the associated records. Include a bind params called ‘%ids’ for the foreign/parent ids.

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

    optional - Ruby modules to include in the result objects (single or array)

  • binds (Hash) (defaults to: {})

    any additional binds for your query.

  • model (ActiveRecord::Base) (defaults to: nil)

    optional - ActiveRecord model that represents what you’re loading. required when using Sqlite.


  • eager load associations nested under this one


  • self

# File 'lib/occams-record/eager_loaders/builder.rb', line 118

def eager_load_many(name, mapping, sql, binds: {}, model: nil, use: nil, &builder)
  @eager_loaders <<, mapping, sql, binds: binds, model: model, use: use, &builder)