1

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

Chris
  • 51
  • 6

1 Answers1

1

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.

subvertallchris
  • 5,282
  • 2
  • 25
  • 43
  • I would add a couple of things. You can also do `team.league.create(other_node, property: value)` if you want to put properties on the relationship – Brian Underwood Jun 11 '15 at 17:01
  • The other thing is that I think your use of `model_class` in your association declaration isn't right. For example for your `Team.league` association you declare a `model_class` of `Team`. The `model_class`, though, refers to the target of the association so it should be `League`. But you actually don't need to specify a `model_class` in this case because the association name `:league` will auto-guess `League` as the `model_class`. The same goes for the association in the other model – Brian Underwood Jun 11 '15 at 17:04
  • Awesome, thanks guys! I removed the `model_class`. I also read through the wiki, you guys have put up a ton of great info! I understand it in theory but I'm still a bit confused here when it comes to creating a relationship in practice. I think this is in part because I'm still thinking in cypher. – Chris Jun 11 '15 at 23:02
  • Bear with me here. If we say that in cypher `CREATE (n:League {name: "League 1"})` is equal to the following code in the rails console run against the above rails models: `League.create(name: "League 2")`, how would I jump from there to `MATCH (n:Team {name: "Team 2"}), (m:League {name: "League 2"}) CREATE (n)-[r:PLAY_IN]->(m)`? How does one specify the nodes when you do `league.teams << team`? – Chris Jun 11 '15 at 23:09
  • Also, slightly separate question... when I run `league.teams << team` I get `NameError: undefined local variable or method `league' for main:Object`. I know I'm missing something here, but I'm not sure what. – Chris Jun 11 '15 at 23:15
  • Oops. Typo in my second comment. The first block of cypher code should be `CREATE (n:League {name: "League 2"})` – Chris Jun 11 '15 at 23:18
  • Why don't you stop by https://gitter.im/neo4jrb/neo4j sometime tomorrow, Brian and I are usually around all day and can help you out. – subvertallchris Jun 12 '15 at 00:00
  • But to answer your specific questions, check out https://gist.github.com/subvertallchris/ce2b9a7415865a166467 for some examples. `team` and `league` need to both be instances of nodes. – subvertallchris Jun 12 '15 at 00:06
  • Ah, figured it out! First, match two nodes: `team_2 = Team.where(name: "Team 2").first` and `league_2 = League.where(name: "League 2").first`. Then create a relationship between the two: `team_2.create_rel(:PLAY_IN, league_2)` – Chris Jun 12 '15 at 16:38