Skip to content
subvertallchris edited this page Sep 4, 2014 · 5 revisions

Optimized Methods

A few enumerable methods have been optimized for Neo4j to move behavior to the server. They all work as instance methods on associations and QueryProxy objects but also classes. These include:

  • 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.

# 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.

```ruby
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. It is mostly used with queries to determine whether any nodes match a given condition.
```ruby
s.lessons.teachers(:t, :r).where(r: { since: 2001 }).exists?
=> true
Clone this wiki locally