Method: ActiveGraph::Node::Query::QueryProxyMethods#find_or_create_by

Defined in:
lib/active_graph/node/query/query_proxy_methods.rb

#find_or_create_by(params) ⇒ Object

When called, this method returns a single node that satisfies the match specified in the params hash. If no existing node is found to satisfy the match, one is created or associated as expected.



152
153
154
155
156
157
158
159
160
161
# File 'lib/active_graph/node/query/query_proxy_methods.rb', line 152

def find_or_create_by(params)
  fail 'Method invalid when called on Class objects' unless source_object
  result = self.where(params).first
  return result unless result.nil?
  ActiveGraph::Base.transaction do
    node = model.create(params)
    self << node
    node
  end
end