Class: OccamsRecord::EagerLoaders::HasOne
- Defined in:
- lib/occams-record/eager_loaders/has_one.rb
Overview
Eager loader for has_one associations.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#merge!(assoc_rows, rows) ⇒ Object
Merge the association rows into the given rows.
-
#query(rows) { ... } ⇒ Object
Yield one or more ActiveRecord::Relation objects to a given block.
Methods inherited from Base
Constructor Details
This class inherits a constructor from OccamsRecord::EagerLoaders::Base
Instance Method Details
#merge!(assoc_rows, rows) ⇒ Object
Merge the association rows into the given rows.
25 26 27 28 |
# File 'lib/occams-record/eager_loaders/has_one.rb', line 25 def merge!(assoc_rows, rows) Merge.new(rows, name). single!(assoc_rows, @ref.active_record_primary_key.to_s, @ref.foreign_key.to_s) end |
#query(rows) { ... } ⇒ Object
Yield one or more ActiveRecord::Relation objects to a given block.
11 12 13 14 15 16 17 |
# File 'lib/occams-record/eager_loaders/has_one.rb', line 11 def query(rows) return if rows.empty? ids = rows.map { |r| r.send @ref.active_record_primary_key }.compact.uniq q = base_scope.where(@ref.foreign_key => ids) q.where!(@ref.type => rows[0].class&.model_name) if @ref.[:as] yield q end |