Skip to content

Neo4j v3 Declared Relationships

Brian Underwood edited this page Aug 7, 2014 · 31 revisions

Declare relationships in your models to create methods for getting, setting, and traversing relationships.

There are a few configurable options possible in all relationship declarations. A basic understanding of Neo4j relationships is helpful, bordering on necessary, so see http://docs.neo4j.org/chunked/stable/graphdb-neo4j-relationships.html.

All has_many/has_one methods begin with declaration of direction

Those can be:

  • :in
  • :out
  • :both

:both comes with the caveat that the relationship created will be outgoing but matches will omit the direction.

Direction is too important to overlook because Neo4j relationships are so different from ActiveRecord, it should be at the forefront.

class Show
  has_many :in, :bands
end

Second parameter is a symbol that sets the method and defaults: relationship type and expected object class

class Show
  has_many :in, :bands
end
  • Method is "bands"
  • Relationship type is "#bands"
  • Looks for constant Band These are only DEFAULTS, though. All can be overridden.

Automatically generated relationship types are prefixed with "#"

In the above example, the type is "#bands". The reason it is that and not just "bands" is because it acts as a reminder that this relationship type was created by ActiveNode. In practice, the idea is that if you come across this relationship through a match, you don't have to wonder what it is or where it came from, since you will not have specified it explicitly anywhere. It signifies that it was automatically generated.

Polymorphic matches and overriding the model

By default, the second parameter defines the methods to be created and tells the association to look for a class of that name. Use the model_class option to specify a target class.

class Show
  has_many :in, :bands
  # looks for Band

  has_many :in, :people_playing_music
  # Uninitialized Constant PeoplePlayingMusic

  has_many :in, :people_playing_music, model_class: Band
  # overrides default
end

To define a polymorphic association, pass model_class boolean false.

class User
  has_many :out, :managed_objects, model_class: false
end

class Show
  has_many :in, :admins, rel_type: '#managed_objects', model_class: User
end

class Band
  has_many :in, :admins, type: '#managed_objects', model_class: User
end

Declaring relationship type

By default, it bases relation type off of the symbol passed. Use rel_type to declare a specific type.

class Show
  has_many :out, :people_playing_instruments, rel_type: 'performing_bands', model_class: Band
end

Declaring Origin

Rather than declaring the full relationship on two models, you can use origin to reference a starting point.

class Show
  include Neo4j::ActiveNode
  has_many :out, :bands, rel_type: 'performing_bands'
  has_one :out, :headliner, model_class: Band, rel_type: 'headliner'
end

class Band
  include Neo4j::ActiveNode
  has_many :in, :shows, origin: :bands
  has_many :in, :headlining_shows, model_class: Show, origin: :headliner
end

The benefit of this is that changing the relationship types declared on Show will not require updates to Band. There is still a dependency -- changing the method on Show will break Band -- but this is testable and easy to catch.

Clone this wiki locally