diff --git a/lib/neo4j.rb b/lib/neo4j.rb index e4f08c330..d894b2eb4 100644 --- a/lib/neo4j.rb +++ b/lib/neo4j.rb @@ -16,6 +16,7 @@ require 'active_support/core_ext/class/attribute.rb' require 'active_attr' +require 'neo4j/errors' require 'neo4j/config' require 'neo4j/wrapper' require 'neo4j/active_rel/rel_wrapper' diff --git a/lib/neo4j/active_node/labels.rb b/lib/neo4j/active_node/labels.rb index d7a6e525e..599a764ba 100644 --- a/lib/neo4j/active_node/labels.rb +++ b/lib/neo4j/active_node/labels.rb @@ -85,11 +85,13 @@ module ClassMethods def find(id) map_id = proc { |object| object.respond_to?(:id) ? object.send(:id) : object } - if id.is_a?(Array) - find_by_ids(id.map { |o| map_id.call(o) }) - else - find_by_id(map_id.call(id)) - end + result = if id.is_a?(Array) + find_by_ids(id.map { |o| map_id.call(o) }) + else + find_by_id(map_id.call(id)) + end + fail Neo4j::RecordNotFound if result.blank? + result end # Finds the first record matching the specified conditions. There is no implied ordering so if order matters, you should specify it yourself. diff --git a/lib/neo4j/active_node/orm_adapter.rb b/lib/neo4j/active_node/orm_adapter.rb index 6145d8ff1..167d365dc 100644 --- a/lib/neo4j/active_node/orm_adapter.rb +++ b/lib/neo4j/active_node/orm_adapter.rb @@ -28,7 +28,7 @@ def get!(id) # Get an instance by id of the model def get(id) - klass.find(wrap_key(id)) + klass.find_by(klass.id_property_name => wrap_key(id)) end # Find the first instance matching conditions diff --git a/lib/neo4j/active_node/query/query_proxy.rb b/lib/neo4j/active_node/query/query_proxy.rb index e35fba3ad..74ddae14a 100644 --- a/lib/neo4j/active_node/query/query_proxy.rb +++ b/lib/neo4j/active_node/query/query_proxy.rb @@ -196,8 +196,8 @@ def rel end def _nodeify!(*args) - other_nodes = [args].flatten.map do |arg| - (arg.is_a?(Integer) || arg.is_a?(String)) ? @model.find(arg) : arg + other_nodes = [args].flatten!.map! do |arg| + (arg.is_a?(Integer) || arg.is_a?(String)) ? @model.find_by(@model.id_property_name => arg) : arg end.compact if @model && other_nodes.any? { |other_node| !other_node.is_a?(@model) } diff --git a/lib/neo4j/errors.rb b/lib/neo4j/errors.rb new file mode 100644 index 000000000..fa69a4ab3 --- /dev/null +++ b/lib/neo4j/errors.rb @@ -0,0 +1,10 @@ +module Neo4j + # Neo4j.rb Errors + # Generic Neo4j.rb exception class. + class Neo4jrbError < StandardError + end + + # Raised when Neo4j.rb cannot find record by given id. + class RecordNotFound < Neo4jrbError + end +end diff --git a/spec/e2e/label_spec.rb b/spec/e2e/label_spec.rb index 4ddd37b35..74800d4b1 100644 --- a/spec/e2e/label_spec.rb +++ b/spec/e2e/label_spec.rb @@ -233,6 +233,12 @@ class Foo2 < Foo1 it 'by object' do clazz.find(object1).should eq(object1) end + + context 'with no results' do + it 'raises an error' do + expect { clazz.find(8_675_309) }.to raise_error { Neo4j::RecordNotFound } + end + end end describe 'finding multiple records' do @@ -243,6 +249,12 @@ class Foo2 < Foo1 it 'by object' do clazz.find([object1, object2]).to_set.should eq([object1, object2].to_set) end + + context 'with no results' do + it 'raises an error' do + expect { clazz.find[8_675_309] }.to raise_error { Neo4j::RecordNotFound } + end + end end end end