Neo4j.rb创造了独特的关系

Raj*_*oit 4 ruby ruby-on-rails neo4j nosql neo4j.rb

这是我的Neo4j主动节点

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
Run Code Online (Sandbox Code Playgroud)

我想创造独特的关系.

为避免重复,我们必须在创建关系密码查询时使用create unique.

例:

MATCH (root { name: 'root' })
CREATE UNIQUE (root)-[:LOVES]-(someone)
RETURN someone
Run Code Online (Sandbox Code Playgroud)

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

我怎么能用Rails在Neo4j.rb中做到这一点......?

提前致谢..

mrs*_*tif 6

就像更新一样,您现在可以执行以下操作:

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

class User
  include Neo4j::ActiveNode
  has_many :out, :following, type: :following, model_class: 'User', unique: true
end
Run Code Online (Sandbox Code Playgroud)

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

class Following
  include Neo4j::ActiveRel

  creates_unique

  from_class User
  to_class   User
end
Run Code Online (Sandbox Code Playgroud)


Bri*_*ood 5

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

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

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

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
Run Code Online (Sandbox Code Playgroud)

编辑:请参阅mrstif的答案以获取更新