这是我的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
我想创造独特的关系。
为避免重复,我们必须在创建关系密码查询时使用create unique。
示例:
MATCH (root { name: 'root' })
CREATE UNIQUE (root)-[:LOVES]-(someone)
RETURN someone
参考:http://neo4j.com/docs/stable/query-create-unique.html
如何使用Rails在Neo4j.rb中执行此操作?
提前致谢..
答案 0 :(得分:6)
就像更新一样,您现在可以执行以下操作:
对于简单关系,请使用unique:true
:
class User
include Neo4j::ActiveNode
has_many :out, :following, type: :following, model_class: 'User', unique: true
end
对于声明的关系,请使用creates_unique
:
class Following
include Neo4j::ActiveRel
creates_unique
from_class User
to_class User
end
答案 1 :(得分:4)
这是我们有一个问题:
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
编辑:请参阅mrstif的更新答案