Skip to content
subvertallchris edited this page Nov 15, 2014 · 5 revisions

Optimized Methods

A few enumerable methods have been optimized for Neo4j to move behavior to the server and others have been added or modified from their ActiveRecord counterparts to be more Neo4j-y. The best place to see them in action is the query_proxy_methods_spec.rb file, currently right here, and the query_proxy_methods.rb file itself, currently here, but some more info is below.

We are always looking for more methods to simplify common queries or push behavior to the database when possible. If you come across something you think would be helpful, please send a PR or open an issue!

  • count

Accepts a parameter of :distinct to count distinct nodes. Works on classes and QueryProxy chains.

s = Student.first
s.lessons.count
=> 3
s.lessons.count(:distinct)
=> 1
s.lessons.students.where(age: '21').count
=> 12

Optional second parameter, a symbol matching an existing identifier in your QueryProxy chain, to specify a target of the count.

It is aliased to size and length.

# Starting from a specific student, count (non-distinct) their lessons that have other students of age 21.
s.lessons(:l).students.where(age: '21').count(nil, :l)
  • include?

include? expects a node.

Student.include?(s)
=> true
s.lessons.teachers(:t, :r).where(r: {since: 2001}).include?(a_teacher)
=> true

Optional second parameter of an identifier to look for inclusion in an earlier part of the match.

# Is this matched by this query? 
s.lessons(:l).teachers(:t, :r).where(r: {since: 2001}).include?(a_lesson, :l)
  • exists?

exists? runs with or without a parameter, which is expected to be a neo_id OR a hash, {property: 'value'}. It is mostly used with queries to determine whether any nodes match a given condition.

s.lessons.teachers(:t, :r).where(r: { since: 2001 }).exists?
s.lessons.teachers.exists?(name: 'Mr. Rogers')
=> true

Also accepts a second optional parameter, a symbol matching an earlier portion of the query proxy chain.

  • empty?

It does exactly what you think it does. It is also aliased to blank?.

  • delete_all

This performs a serverside delete on the last hop of the QueryProxy chain. It accepts an optional parameter, the Cypher identifier of a node or rel.

# Delete all teachers of a given student's lessons
student.lessons.teachers.delete_all

# Delete all relationships between lessons and teachers of a given student
student.lessons(:l, :r).teachers.delete_all(:r)

# Delete all lessons taught by Mr Jones in which a given student is enrolled
student.lessons(:l).teachers.where(name: 'Mr Jones').delete_all(:l)
  • match_to(node)

This is shorthand for query_proxy_chain.where(neo_id: node.neo_id). It returns a QueryProxy chain.

# Get all the teachers of a particular student's particular science class
student.lessons.match_to(science).teachers
  • first_rel_to(node)

Shorthand for query_proxy_chain.where(neo_id: node.neo_id).limit(1).pluck(rel_identity).first. It returns the first (the first one found, not necessarily the first created) relationship between a given node and its previous link within a QueryProxy chain.

# Get the relationship between Mr Jones and the science class he is teaching in which our student is enrolled.
student.lessons.match_to(science).teachers.first_rel_to(mr_jones)
# This generates Cypher that looks like this:
#  MATCH (student1775:`Student`), (node2:`Lesson`), student1775-[rel0:`ENROLLED_IN`]->(node2:`Lesson`), (result:`Teacher`), node2<-[rel1:`lessons_taught`]-(result:`Teacher`) WHERE ID(student1775) = {ID_student1775} AND ID(node2) = 1774 AND ID(result) = 1773 RETURN rel1 LIMIT 1
Clone this wiki locally