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



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

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

#precreate(value) ⇒ Object



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

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