Method: OccamsRecord::EagerLoaders::Builder#eager_load_many
- Defined in:
- lib/occams-record/eager_loaders/builder.rb
#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
.query(Widget.order("name"))
.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"]
})
.run
118 119 120 121 |
# 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 << EagerLoaders::AdHocMany.new(name, mapping, sql, binds: binds, model: model, use: use, &builder) self end |