Class: OccamsRecord::EagerLoaders::HasOne

Inherits:
Base
  • Object
show all
Defined in:
lib/occams-record/eager_loaders/has_one.rb

Overview

Eager loader for has_one associations.

Direct Known Subclasses

HasMany

Instance Attribute Summary

Attributes inherited from Base

#eval_block, #name, #use

Instance Method Summary collapse

Methods inherited from Base

#initialize

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.

Parameters:



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.

Parameters:

Yields:



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.options[:as]
  yield q
end