Class: OccamsRecord::EagerLoaders::BelongsTo

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

Overview

Eager loader for belongs_to associations.

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.



22
23
24
25
# File 'lib/occams-record/eager_loaders/belongs_to.rb', line 22

def merge!(assoc_rows, rows)
  Merge.new(rows, name).
    single!(assoc_rows, @ref.foreign_key.to_s, @model.primary_key.to_s)
end

#query(rows) { ... } ⇒ Object

Yield one or more ActiveRecord::Relation objects to a given block.

Yields:



11
12
13
14
# File 'lib/occams-record/eager_loaders/belongs_to.rb', line 11

def query(rows)
  ids = rows.map { |r| r.send @ref.foreign_key }.compact.uniq
  yield base_scope.where(@ref.active_record_primary_key => ids)
end