Class: DataloaderRelationProxy::ActiveRecordObject

Inherits:
GraphQL::Dataloader::Source
  • Object
show all
Defined in:
lib/dataloader_relation_proxy/active_record_object.rb

Overview

Dataloader that loads ActiveRecord objects by an arbitrary key

Instance Method Summary collapse

Constructor Details

#initialize(model_class, key = :id) ⇒ ActiveRecordObject

Returns a new instance of ActiveRecordObject.



6
7
8
9
10
# File 'lib/dataloader_relation_proxy/active_record_object.rb', line 6

def initialize(model_class, key = :id)
  super()
  @model_class = model_class
  @key = key
end

Instance Method Details

#fetch(ids) ⇒ Object



12
13
14
15
16
# File 'lib/dataloader_relation_proxy/active_record_object.rb', line 12

def fetch(ids)
  records = @model_class.where(@key => ids)
  by_id = records.index_by { |record| record.send(@key) }
  ids.map { |id| by_id[id] }
end