Module: OccamsRecord::EagerLoaders::Builder
Overview
Methods for adding eager loading to a query.
Instance Method Summary collapse
-
#eager_load(assoc, scope = nil, select: nil, use: nil, as: nil) { ... } ⇒ OccamsRecord::Query
Specify an association to be eager-loaded.
Instance Method Details
#eager_load(assoc, scope = nil, select: nil, use: nil, as: nil) { ... } ⇒ OccamsRecord::Query
Specify an association to be eager-loaded. For maximum memory savings, only SELECT the colums you actually need.
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.
28 29 30 31 32 33 34 35 |
# File 'lib/occams-record/eager_loaders.rb', line 28 def eager_load(assoc, scope = nil, select: nil, use: nil, as: nil, &eval_block) ref = @model ? @model.reflections[assoc.to_s] : nil ref ||= @model.subclasses.map(&:reflections).detect { |x| x.has_key? assoc.to_s }&.[](assoc.to_s) if @model raise "OccamsRecord: No assocation `:#{assoc}` on `#{@model&.name || '<model missing>'}` or subclasses" if ref.nil? scope ||= ->(q) { q.select select } if select @eager_loaders << eager_loader_for_association(ref).new(ref, scope, use: use, as: as, &eval_block) self end |