Skip to content
Chris Grigg edited this page Mar 26, 2015 · 25 revisions

Include in your gemfile:

# for rubygems
gem 'neo4j', '~> 4.1.1'
# for master, which is typically stable:
gem 'neo4j', github: 'neo4jrb/neo4j'

If using Rails, include the railtie in application.rb:

require 'neo4j/railtie'

To use the model generator, modify application.rb once more:

class Application < Rails::Application
  config.generators { |g| g.orm :neo4j }     
end

If not using Rails, include the rake tasks in your Rakefile:

load 'neo4j/tasks/neo4j_server.rake'
load 'neo4j/tasks/migration.rake'

Usage from Ruby

Rake tasks and basic server connection are defined in the neo4j-core gem. See its documentation for more details.

With the Rake tasks loaded, install Neo4j and start the server:

rake neo4j:install[community-2.2.0]
rake neo4j:start

(Note that if you are using zsh, you need to prefix any rake tasks with arguments with the noglob command, e.g. $ noglob bundle exec rake neo4j:install[community-2.2.0-M02].)

At this point, it will give you a message that the server has started or an error. Assuming everything is ok, point your browser to http://localhost:7474 and the Neo4j web console should load up.

To open a session to the neo4j server database:

# In JRuby or MRI, using Neo4j Server mode. When the railtie is included, this happens automatically.
Neo4j::Session.open(:server_db)

After you have created a session you can now use the database, see below.

Usage from JRuby

On JRuby you can access the database in two different ways: using the embedded db or the server db.

Example, Open a session to the neo4j embedded database (running in the same JVM)

  session = Neo4j::Session.open(:embedded_db, '/folder/db')
  session.start

Usage with a remote Server

Example of a rails config/application.rb file:

config.neo4j.session_options = { basic_auth: { username: 'foo', password: 'bar'} } 
config.neo4j.session_type = :server_db 
config.neo4j.session_path = 'http://localhost:7474'

For more configuration options, use the initialize session option parameter which is used to initialize a Faraday session.

Example: config.neo4j.session_options = {initialize: { ssl: { verify: true }}

See https://gist.github.com/andreasronge/11189170 how to configure the Neo4j::Session with basic authentication from a non-rails application.

A _classname property is added to all nodes during creation to store the object's class name. This prevents an extra query to the database when wrapping the node in a Ruby class. To change the property name, add this to application.rb:

config.neo4j[:class_name_property] = :new_name

NOTE The above is not true when using the master branch and Neo4j v2.1.5 or greater. See https://github.com/neo4jrb/neo4j/wiki/Neo4j.rb-v4-Introduction for more info.

Usage from heroku

Add a Neo4j db to your application:

# Substitute "chalk" with the plan of your choice
heroku addons:add graphenedb:chalk

See https://devcenter.heroku.com/articles/graphenedb for more info, https://addons.heroku.com/graphenedb for plans.

Example of a rails config/application.rb file:

config.neo4j.session_type = :server_db 
config.neo4j.session_path = ENV["GRAPHENEDB_URL"] || 'http://localhost:7474'

Usage from Rails

 rails new myapp -m http://neo4jrb.io/neo4j/neo4j.rb -O
 cd myapp
 rake neo4j:install[community-2.1.6]
 rake neo4j:start

 rails generate scaffold User name:string email:string
 rails s
 open http://localhost:3000/users

Or manually modify the rails config file config/application.rb:

require 'neo4j/railtie'

module Blog
  class Application < Rails::Application
     # To use generators:
     config.generators { |g| g.orm :neo4j }
     # This is for embedded db, only available from JRuby
     #config.neo4j.session_type = :embedded_db # or server_db
     #config.neo4j.session_path = File.expand_path('neo4j-db', Rails.root) # or http://localhost:port
  end
end

You can skip Active Record by using the -O flag when generating the rails project.

Clone this wiki locally