简体   繁体   English

如何通过Rails控制台创建Neo4j关系?

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

I'm currently working through this tutorial and I'm stuck when it comes to creating relationships in the rails console. 我目前正在本教程中学习 ,但在Rails控制台中创建关系时仍然很困惑。 I've read through the Neo4jrb project documentation and a blog post on jayway.com but still can't figure it out. 我已经阅读了Neo4jrb项目文档和jayway.com上的博客文章,但仍然无法弄清。

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. 我已经创建了一个Rails站点,并且想使用我的Rails脚本在Neo4j数据库中创建团队节点,联赛节点以及它们之间的关系。 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: 使用rails控制台,我可以使用以下代码创建节点:

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. 这是我在github中的代码。 Thanks in advance! 提前致谢!

** Edit ** **编辑**

Removed: model_class 删除:model_class

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 . 在Wiki的ActiveNode部分的Associations标题下有一个在节点之间创建关系的示例, 网址为https://github.com/neo4jrb/neo4j/wiki/Neo4j%3A%3AActiveNode#associations You do node_a.association_name << node_b . 您执行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. team.league = league and league.teams << team将创建相同的关系,因为您已将它们设置为引用数据库中相同的关系类型和相互关系。

There's a ton of information in the wiki, I suggest you read through all the modern stuff. Wiki中有大量信息,我建议您通读所有现代文章。 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. http://neo4jrb.readthedocs.org/en/stable/上也正在研究新文档,但仍有很多工作要做。 There's also a chat room at https://gitter.im/neo4jrb/neo4j , in case you ever want to talk through something. https://gitter.im/neo4jrb/neo4j上还有一个聊天室,以防您想与他人交谈。

** EDIT ** **编辑**

As Brian pointed out, there's an issue with your model_class . 正如Brian指出的那样,您的model_class存在问题。 I was focusing on how you do it and didn't look too closely at the models, see his comments for info. 我一直在关注您的操作方式,但并没有对模型进行过于仔细的研究,请参阅他的评论以获取信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM