首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Neo4j.rb创建独特的关系

Neo4j.rb创建独特的关系
EN

Stack Overflow用户
提问于 2015-01-07 09:57:21
回答 2查看 831关注 0票数 5

这是我的Neo4j活动节点

代码语言:javascript
复制
class User
include Neo4j::ActiveNode
  has_many :out, :following, type: :following, model_class: 'User'
end

john = User.find(:name => "John")
tom = User.find(:name => "Tom")

# create following relationship john --> tom
john.following << tom
# check count
john.following.count 
#=> 1

# again create the relationship 
john.following << tom
# again check count
john.following.count
#=> 2

我想要创造独特的关系。

为了避免重复,我们必须在创建关系密码查询时使用create唯一。

示例:

代码语言:javascript
复制
MATCH (root { name: 'root' })
CREATE UNIQUE (root)-[:LOVES]-(someone)
RETURN someone

参考:http://neo4j.com/docs/stable/query-create-unique.html

我怎么能用Rails在Neo4j.rb里做这件事.?

提前谢谢..。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-07 11:59:35

这是我们面临的一个问题:

https://github.com/neo4jrb/neo4j/issues/473

现在,我建议在User模型上创建这样的方法:

代码语言:javascript
复制
def create_unique_follower(other)
    Neo4j::Query.match(user: {User: {neo_id: self.neo_id}})
                .match(other: {User: {neo_id: other.neo_id}})
                .create_unique('user-[:following]->other').exec
end

编辑:有关更新,请参阅mrstif的答案

票数 5
EN

Stack Overflow用户

发布于 2015-10-15 16:18:51

作为一个更新,您现在可以执行以下操作:

对于简单的关系,请使用unique:true

代码语言:javascript
复制
class User
  include Neo4j::ActiveNode
  has_many :out, :following, type: :following, model_class: 'User', unique: true
end

对于声明的关系,请使用creates_unique

代码语言:javascript
复制
class Following
  include Neo4j::ActiveRel

  creates_unique

  from_class User
  to_class   User
end
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27816748

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档