Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Neo4j + RoR

Neo4j + RoR

Using Neo4j in a Ruby on Rails project

Regina Imhoff

June 05, 2017
Tweet

More Decks by Regina Imhoff

Other Decks in Programming

Transcript

  1. What is Neo4j • Graph database ◦ NoSQL ◦ Schema

    free - you can make it up and change it as you go! ◦ Index-free adjacency == short read time ◦ Collection of nodes, edges, and properties to represent and store data • Written in Java but available in most languages • Most popular graph database • Scalable and agile • Free, open source community version and paid support version • Uses Cypher for querying language ◦ Neo4j offers a Cypher cheat sheet - use it! ◦ http://neo4j.com/docs/pdf/neo4j-cypher-refcard-stable.pdf
  2. What does the database look like? • Node ◦ AKA

    vertex • Relationship ◦ AKA edge • Property ◦ AKA attribute
  3. In my app: • Users own containers and containers contain

    items • Multiple users can have the same container (roommates/SOs/etc) ◦ A social network of users keeping track of their stuff! • Can have infinite number of containers & items ◦ Awesome Nested Set gem can do this too without a graph database!
  4. How does it work with Rails? • gem ‘neo4j’ ◦

    Active Model wrapper for Neo4j • Other gems may not work with it or need different versions ◦ gem ‘carrierwave-neo4j’ for image uploads • Can use free community edition of Neo4j • Works on Heroku, AWS, etc. • Models look a little different
  5. Container model class Container include Neo4j::ActiveNode # Attributes property :created_at,

    type: DateTime property :name, type: String property :description, type: String property :updated_at, type: DateTime # Containers has_one :in, :parent, type: :CONTAINS, model_class: :Container has_many :out, :items, type: :CONTAINS has_many :out, :containers, type: :CONTAINS