How do I create a Neo4j relationship via the rails console?

497 views Asked by At

I'm currently working through this tutorial and I'm stuck when it comes to creating relationships in the rails console. I've read through the Neo4jrb project documentation and a blog post on jayway.com but still can't figure it out.

I've created a rails site and I want to create team nodes, league nodes, and relationships between them in a Neo4j database using my rails scripts. I have two models:

One for League

class Team 
include Neo4j::ActiveNode
property :name, type: String

has_one :out, :league, type: :PLAY_IN

end

One for Team

class League 
include Neo4j::ActiveNode
property :name, type: String
property :rank, type: Integer

has_many :in, :teams, origin: :league

end

Using the rails console, I can create a node using this code:

League.create(name: "League 2")

Using the console, how do I create a relationship between two nodes as defined in my models?

Here is my code in github. Thanks in advance!

** Edit **

Removed: model_class

1

There are 1 answers

9
subvertallchris On BEST ANSWER

There's an example of creating a relationship between nodes under the Associations heading of the ActiveNode section of the wiki, https://github.com/neo4jrb/neo4j/wiki/Neo4j%3A%3AActiveNode#associations. You do node_a.association_name << node_b. team.league = league and league.teams << team will create the same relationship since you've set them up to refer to the same relationship type and reciprocal directions in the database.

There's a ton of information in the wiki, I suggest you read through all the modern stuff. Don't worry about anything in the "Legacy" section. New docs are also being worked on at http://neo4jrb.readthedocs.org/en/stable/ but there's still a bit to do. There's also a chat room at https://gitter.im/neo4jrb/neo4j, in case you ever want to talk through something.

** EDIT **

As Brian pointed out, there's an issue with your model_class. I was focusing on how you do it and didn't look too closely at the models, see his comments for info.