lib/architect4r/model/queries.rb in architect4r-0.3.3.1 vs lib/architect4r/model/queries.rb in architect4r-0.3.4
- old
+ new
@@ -9,25 +9,27 @@
module ClassMethods
def count(opts = {}, &block)
data = connection.execute_cypher("start s=node(#{self.model_root.id}) match (s)<-[:model_type]-(d) return count(d)")
- data.first['count(d)']
+ data['data'].flatten.first
end
def find_by_id(id)
data = connection.execute_cypher("start s=node(#{self.model_root.id}), d=node(#{id.to_i}) match s<-[r:model_type]-d return d")
- data &&= data.first && data.first['d']
+ data &&= data['data'] && data['data'].flatten.first
self.build_from_database(data)
end
def find_by_id!(id)
- raise 'not implemented'
+ self.find_by_id(id) || raise(Architect4r::RecordNotFound.new("Could not find the #{self.name} with id #{id}!"))
end
+ # Use this method only to fetch items of the same class.
def find_by_cypher(query, identifier)
- if data = connection.execute_cypher(query)
- data.map { |item| build_from_database(item[identifier]) }
+ if result_data = connection.execute_cypher(query)
+ result_data = connection.transform_cypher_result_to_hash(result_data)
+ result_data.map { |item| connection.convert_if_possible(item[identifier]) }
else
nil
end
end
\ No newline at end of file