如何通过Rails控制台创建Neo4j关系? [英] How do I create a Neo4j relationship via the rails console?

查看:88
本文介绍了如何通过Rails控制台创建Neo4j关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在通过本教程工作,当我陷入困境时在Rails控制台中创建关系.我已经阅读了Neo4jrb项目文档和jayway.com上的博客文章,但仍然无法弄清.

我已经创建了一个Rails站点,我想使用Rails脚本在Neo4j数据库中创建团队节点,联赛节点以及它们之间的关系.我有两种型号:

一个联赛

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

has_one :out, :league, type: :PLAY_IN

end

一个团队成员

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

has_many :in, :teams, origin: :league

end

使用rails控制台,我可以使用以下代码创建节点:

League.create(name: "League 2")

如何使用控制台,如何在模型中定义的两个节点之间建立关系?

这是我在github中的代码.预先感谢! /p>

**编辑**

已删除:model_class

解决方案

在Wiki的ActiveNode部分的Associations标题下,有一个在节点之间创建关系的示例, http://neo4jrb.readthedocs.org/en/stable/,但是还有很多事情要做.如果您愿意,在 https://gitter.im/neo4jrb/neo4j 处还有一个聊天室.说话.

**编辑**

正如布赖恩指出的那样,您的model_class存在问题.我一直在关注您的操作方式,但并不太仔细地研究模型,请参阅他的评论以获取信息.

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

解决方案

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.

这篇关于如何通过Rails控制台创建Neo4j关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆