This ruby on rails gem makes it easy to store polymorphic structure information—parents, children, ancestors, descendants, …—in a neo4j graph database parallel to using ActiveRecord.
MIT License
This ruby on rails gem makes it easy to store polymorphic structure information—parents
, children
, ancestors
, descendants
, …—in a neo4j graph database parallel to using ActiveRecord.
All relevant information is stored in your default ActiveRecord database, including the parent-child relationships. But, in addition, the structure information is also stored in a neo4j graph database in order to use its power of fast graph traversing queries.
TODO: Write usage instructions here
has_neo4j_ancestry
in nodesancestors
or ancestor_groups
find_shortest_path_to(other_node)
.You can use Neo4j's excellent web interface when the daemon is running:
From there, you can use Cypher queries like this producing the above:
match
(group1:Group {active_record_id: 2978}),
(group2:Group {active_record_id: 2983}),
paths = (group1)-[:is_parent_of*1..100]->(group2)
return paths
order by length(paths)
This is an excellen cheat sheet for cypher: http://neo4j.com/docs/2.0/cypher-refcard/
First, make sure, Java 7 JDK is installed. You can check using java -version
. If you need to install it:
Mac OS
Install the JDK, not the JRE: Download
Set your JAVA_HOME
environment variable in the .zshenv
or .bashrc
like this:
# .zshenv
export JAVA_HOME=`/usr/libexec/java_home -v 1.7`
Ubuntu/Debian GNU Linux
aptitude install openjdk-7-jre-headless
Add the gem to your application's Gemfile
:
# Gemfile
# ...
gem 'neo4j_ancestry'
And then execute:
# bash
bundle install
This will install two instances of the neo4j database inside the db
directory of your app—one instance for development
, one for test
. The data of the database instances are stored in data
subdirectories.
# bash
bundle exec rake neo4j:install neo4j:get_spatial neo4j:setup neo4j:start
Next, migrate the ActiveRecord database in order to add a table for direct links between objects. This way, all relevant information is stored within the ActiveRecord database. This way, the whole graph database could be reconstructed from the ActiveRecord database.
# bash
bundle exec rake neo4j_ancestry:install:migrations
bundle exec rake db:migrate
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)Symlink the Gemfile
according to the Rails version you would like to use:
# bash
rm Gemfile Gemfile.lock
ln -s rails3.2.Gemfile Gemfile # for Rails 3
ln -s rails4.Gemfile Gemfile # for Rails 4 (default)
Next, install the dependencies and run the specs.
# bash
bundle install
bundle exec rake neo4j_ancestry:db:test:prepare
bundle exec rake
© 2013-2014, Sebastian Fiedlschuster
Released under the MIT License.