-
Notifications
You must be signed in to change notification settings - Fork 276
Neo4j v3 Declared Relationships
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.
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.
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.
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
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
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.
WARNING: Much of the information in this wiki is out of date. We are in the process of moving things to readthedocs
- Project Introduction
- Neo4j::ActiveNode
- Neo4j::ActiveRel
- Search and Scope
- Validation, Uniqueness, and Case Sensitivity
- Indexing VS Legacy Indexing
- Optimized Methods
- Inheritance
- Core: Nodes & Rels
- Introduction
- Persistence
- Find : Lucene
- Relationships
- Third Party Gems & extensions
- Scaffolding & Generators
- HA Cluster