sutee
sutee

Reputation: 12828

Neo4j self-relation

What's the proper way to create a relation from an instance of a Neo4j class to another instance of that class?

For example, if I am modeling courses in a course catalog with a model for courses that are prereqs for other courses.

I am using neo4j with rails:

Model:

class Course < Neo4j::Rails::Model property :name
has_n(:prereqs).from(Course, :leadstos) has_n(:leadstos)

Creating objects and relation:

algebra = Course.create :name => 'algebra'
arithmetic = Course.create :name => 'arithmetic'
algebra.prereqs << arithmetic 
algebra.save!
arithmetic.save!

algebra.prereqs.each {|node| puts node [:name]}

#prints 'arithmetic'

However, arithmetic.leadstos.each {|node| puts node[:name]} comes out as blank.

Upvotes: 0

Views: 270

Answers (1)

Deepak N
Deepak N

Reputation: 2571

You will have to declare :leadtos relation as

has_n(:leadstos).to(Course)

Upvotes: 2

Related Questions