Class: Graphoid::BelongsTo

Inherits:
Relation show all
Defined in:
lib/graphoid/operators/inherited/belongs_to.rb

Instance Attribute Summary

Attributes inherited from Relation

#inverse_name, #klass, #name, #type

Instance Method Summary collapse

Methods inherited from Relation

#belongs?, #create, #embedded?, #initialize, #many?, #many_to_many?, #one?, #relation?, relations_of, #resolve

Constructor Details

This class inherits a constructor from Graphoid::Relation

Instance Method Details

#exec(_, value) ⇒ Object



9
10
11
12
13
# File 'lib/graphoid/operators/inherited/belongs_to.rb', line 9

def exec(_, value)
  ids = Graphoid::Queries::Processor.execute(self.klass, value).to_a.map(&:id)
  attribute = Attribute.new(name: "#{self.name.underscore}_id", type: nil)
  Graphoid.driver.parse(attribute, ids, "in")
end

#precreate(value) ⇒ Object



3
4
5
6
7
# File 'lib/graphoid/operators/inherited/belongs_to.rb', line 3

def precreate(value)
  sanitized = Attribute.correct(self.klass, value)
  foreign_id = self.klass.create!(sanitized).id
  { :"#{self.name}_id" => foreign_id }
end