Class: DataloaderRelationProxy::ActiveRecordObject
- Inherits:
-
GraphQL::Dataloader::Source
- Object
- GraphQL::Dataloader::Source
- DataloaderRelationProxy::ActiveRecordObject
- Defined in:
- lib/dataloader_relation_proxy/active_record_object.rb
Overview
Dataloader that loads ActiveRecord objects by an arbitrary key
Instance Method Summary collapse
- #fetch(ids) ⇒ Object
-
#initialize(model_class, key = :id) ⇒ ActiveRecordObject
constructor
A new instance of ActiveRecordObject.
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 |