Skip to content
Andreas Ronge edited this page Aug 28, 2014 · 6 revisions

ActiveRecord styled scope

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