Developer Guides Getting Started Getting Started What is a Graph Database? Intro to Graph DBs Video Series Concepts: RDBMS to Graph Concepts: NoSQL to Graph Getting Started Resources Neo4j Graph Platform Graph Platform Overview Neo4j Desktop Intro Neo4j Browser Intro… Read more →

Developer Guides

Want to Speak? Get $ back.

Using Neo4j from Ruby

Goals
This guide provides an overview of options for connecting to Neo4j using Ruby. It will introduce the different drivers and link to relevant resources.
Prerequisites
You should be familiar with graph database concepts and the property graph model. You should have installed Neo4j and made yourself familiar with our Cypher Query language.
Intermediate

Neo4j Community Drivers

Members of the each programming language community have invested a lot of time and love to develop each one of the community drivers for Neo4j, so if you use any one of them, please provide feedback to the authors.

The community drivers have been graciously contributed by the Neo4j community. Many of them are fully featured and well-maintained, but some may not be. Neo4j does not take any responsibility for their usability.

Neo4j for Rubyists

Neo4j has been accessible from Ruby long before version 1.0, first using JRuby on the JVM and then on MRI via the HTTP and now Bolt protocols.

Neo4j.rb

The Neo4j.rb project is made up of the following Ruby gems:

neo4j

A Object-Graph-Mapper (OGM) for the Neo4j graph database. It tries to follow API conventions established by ActiveRecord but with a Neo4j flavor.

neo4j-core

A low level driver for connecting to Neo4j. Used by the neo4j gem.

neo4j-rake_tasks

A set of rake tasks for installing and managing a Neo4j database within your project. Used by the neo4j-core gem.

Website

http://neo4jrb.io/

Authors

Heinrich, Brian, Chris, Andreas

Package

https://rubygems.org/search?utf8=%E2%9C%93&query=neo4j

Source

https://github.com/neo4jrb

Example

https://github.com/neo4j-examples?q=movies-ruby

Docs

http://neo4jrb.readthedocs.org/en/latest/

Tutorial

Ruby Tutorial

Blog

http://blog.brian-underwood.codes/

Protocols

Bolt, Http

History:

  1. Andreas Ronge, one of our Swedish friends from Malmö, started writing his canonical Neo4j Ruby driver since before we hit 1.0.
  2. Brian Underwood and Chris Grigg joined the project and together released version 3.0 in September 2014.
  3. Starting in 2017, the team around Heinrich Klobuczek contributed to the project and from fall 2018 Heinrich took over the Neo4j.rb project as the primary maintainer.

Gem: Neo4j-core

Neo4j-core is a gem that provides low-level communication to a Neo4j server. In addition to basic database communicaton and node/relationship CRUD, Neo4j-core provides strong support for Neo4j’s schema indexes, constraints, labels, and a strong Cypher DSL.

How to get running:

session = Neo4j::Session.open(:server_db,
                              "http://neo4j:pass@localhost:7474")

session.query("MATCH (person:Person) RETURN person LIMIT 10")


# alternatively you can use the query chaining API:
session.query.match(person: :Person).return(:person).limit(10).to_a

Gem: Neo4j

The neo4j gem uses neo4j-core as an API to connect to the server and provides an ActiveRecord-like experience for use in frameworks. It adds modules allowing the creation of models that look and feel very similar to those found in vanilla Ruby on Rails. The gem builds on Neo4j-core’s foundation to streamline all aspects of node and relationship CRUD and provides an extremely advanced, intuitive, flexible DSL for generating Cypher.

How to get running:

# See the documentation for setup instructions

class Person
  include Neo4j::ActiveNode

  property :name

  has_many :out, :books, type: :OWNS_BOOK
  has_many :both, :friends, type: :HAS_FRIEND
end

person = Person.find_by(name: 'Jim')

# Get the books owned by Jim's friends:
person.friends.books.to_a

Deployment Considerations

Very often, your gem choice may come down to how you want to deploy:

server

Using Ruby with a separate Neo4j Server over the HTTP API

embedded

Connecting directly to the Neo4j database files from within your Ruby process (this requires JRuby)

A separate Neo4j server is go-to choice for most developers, especially if they are used to other relational or NoSQL databases in frameworks such as Ruby on Rails.

It allows you to have separate web and database servers and allows compatibility with popular PaaSes such as Heroku. If this sounds good to you, any of the popular gems are solid choices and you are free to consider whether you want a thin wrapper or the full framework experience.

Neo4j Embedded and JRuby is less common but offers blazing fast performance and access to our core Java API. The downside is that JRuby has its own configuration and compatibility demands and hosting a Java app can be difficult. Thankfully, modern app servers such as Torquebox and a strong community provide far more options and resources now than ever before. If this is right for you, the Neo4j-core and Neo4j gems will offer what you need.

Neo4j gem integrations

There are many common gems that you’ll want to use with your Neo4j database. Many are supported for the Neo4j.rb project:

Authentication

File Attachment

Pagination

ElasticSearch Integration

Admin User Interface

Integration With the Neo4j Spatial Plugin

Ruby Object Manager

Misc.

The Example Project

The Neo4j example project is a small, one page webapp for the movies database built into the Neo4j tutorial. The front-end page is the same for all drivers: movie search, movie details, and a graph visualization of actors and movies. Each backend implementation shows you how to connect to Neo4j from each of the different languages and drivers.

You can learn more about our small, consistent example project across many different language drivers here. You will find the implementations for all drivers as individual GitHub repositories, which you can clone and deploy directly.