Class: Graphoid::HasOne

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

Instance Attribute Summary

Attributes inherited from Relation

#inverse_name, #klass, #name, #type

Instance Method Summary collapse

Methods inherited from Relation

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

Constructor Details

This class inherits a constructor from Graphoid::Relation

Instance Method Details

#create(parent, value, grapho) ⇒ Object



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

def create(parent, value, grapho)
  attributes = Attribute.correct(self.klass, value)
  attributes[:"#{grapho.name}_id"] = parent.id
  self.klass.create!(attributes)
end

#exec(scope, value) ⇒ Object



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

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