Skip to content
Chris Grigg edited this page Aug 29, 2014 · 6 revisions

Scope allows you to reuse queries and make them more readable. It is also possible to combine scope queries with declared relationships, e.g. friends.top_students, see below.

Neo4j.rb implements scope in a similar way to ActiveRecord scope

Example, parameters

class Person
  include Neo4j::ActiveNode
  property :level_num
  scope :level, ->(num){ where(level_num: num)}
end

Person.level(3)

Example, scope chain

class Person
  include Neo4j::ActiveNode
  property :name
  property :score
  property :level_num
  has_many :out, :friends, model_class: self
  scope :top_students, -> { where(score: 1000)}
end

# from one person node
a_person.friends.top_students.friends

# from all person nodes
Person.top_students.friends

Example, order

class Person
  include Neo4j::ActiveNode
  scope :in_order, ->(identifier){ order("#{identifier}.level_num DESC")}
end
Person.as(:people).in_order(:people)
Clone this wiki locally