这是我的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唯一。
示例:
MATCH (root { name: 'root' })
CREATE UNIQUE (root)-[:LOVES]-(someone)
RETURN someone参考:http://neo4j.com/docs/stable/query-create-unique.html
我怎么能用Rails在Neo4j.rb里做这件事.?
提前谢谢..。
发布于 2015-01-07 11:59:35
这是我们面临的一个问题:
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的答案
发布于 2015-10-15 16:18:51
作为一个更新,您现在可以执行以下操作:
对于简单的关系,请使用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
endhttps://stackoverflow.com/questions/27816748
复制相似问题