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 < 1 # again create the relationship john.following < 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中做到这一点……?

提前致谢..

这是我们有一个问题:

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的更新答案

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

对于简单关系,请使用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