-
Notifications
You must be signed in to change notification settings - Fork 276
Declared Relationships
A has_many
declaration is used to generate accessor methods for traversing relationships as well as creating new relationships.
The first argument of the has_many
is the name of the method that will be generated.
Example:
class Person
include Neo4j::ActiveNode
has_many :friends
end
This will generate a friends method on the Person class so that we can create new relationships between two nodes like this:
p = Person.create
p.friends << Person.create # by default it creates an outgoing relationship of type friends
This could also be achieved without using the accessor method, example
p = Person.create
# example 1
p.outgoing(:friends) << Person.create # Notice you don't have to declare the relationship !
# example 2
# See http://www.rubydoc.info/github/andreasronge/neo4j-core/Neo4j/Node#create_rel-instance_method
p.create_rel(:friends, Person.create, {}) # last argument is the properties of the relationship
Example: To retrieve all nodes using the friends
accessor method
p.friends.to_a # [Person object, ...]
It is also possible to retrieve nodes without using the accessor method.
p.outgoing(:friends).to_a
# or, see http://www.rubydoc.info/github/andreasronge/neo4j-core/Neo4j/Node#nodes-instance_method
p.nodes(...)
To retrieve the relationship object instead of the nodes, use the generated friends_rels
method.
Example
p.friends_rels.to_a # [Neo4j::Relationship objects]
# this can also be done using the Neo4j::Node.rels method
The has_many
method has an to
parameter which can be used to generate accessor method for different outgoing relationships.
class Person
include Neo4j::ActiveNode
# map accessor method people_i_know to outgoing relationship :knows
has_many :people_i_know, to: :knows
end
The has_many
method can also be used to generate accessor methods for incoming relationships.
class Person
include Neo4j::ActiveNode
has_many :known_by, from: :knows
end
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