Neo4j.rb is an ActiveRecord-inspired OGM (Object Graph Mapping, like ORM) for Ruby supporting Neo4j 2.1+.
- An Object or Entity which has a distinct identity. Can store arbitrary properties with values
- A means of identifying nodes. Nodes can have zero or more labels. While similar in concept to relational table names, nodes can have multiple labels (i.e. a node could have the labels
- A link from one node to another. Can store arbitrary properties with values. A direction is required but relationships can be traversed bi-directionally without a performance impact.
- Relationships always have exactly one type which describes how it is relating it’s source and destination nodes (i.e. a relationship with a
FRIEND_OFtype might connect two
Neo4j.rb consists of the neo4j and neo4j-core gems.
ActiveRelmodules for object modeling. Introduces Model and Association concepts (see below). Depends on
neo4j-coreand thus both are available when
- Provides low-level connectivity, transactions, and response object wrapping. Includes
Queryclass for generating Cypher queries with Ruby method chaining.
- A Ruby class including either the
Neo4j::ActiveNodemodule (for modeling nodes) or the
Neo4j::ActiveRelmodule (for modeling relationships) from the
neo4jgem. These modules give classes the ability to define properties, associations, validations, and callbacks
- Defined on an
ActiveNodemodel. Defines either a
has_manyrelationship to a model. A higher level abstraction of a Relationship
With Neo4j.rb, you can use either high-level abstractions for convenience or low level APIs for flexibility.
ActiveNode provides an Object Graph Model (OGM) for abstracting Neo4j concepts with an
# Models to create nodes person = Person.create(name: 'James', age: 15) # Get object by attributes person = Person.find_by(name: 'James', age: 15) # Associations to traverse relationships person.houses.map(&:address) # Method-chaining to build and execute queries Person.where(name: 'James').order(age: :desc).first # Query building methods can be chained with associations # Here we get other owners for pre-2005 vehicles owned by the person in question person.vehicles(:v).where('v.year < 2005').owners(:other).to_a