ruby-on-rails – Neo4j.rb创造了独特的关系
发布时间:2020-12-17 03:56:27 所属栏目:百科 来源:网络整理
导读:这是我的Neo4j主动节点 class Userinclude Neo4j::ActiveNode has_many :out,:following,type: :following,model_class: 'User'endjohn = User.find(:name = "John")tom = User.find(:name = "Tom")# create following relationship john -- tomjohn.followi
这是我的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中做到这一点……? 提前致谢.. 解决方法
这是我们有一个问题:
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的更新答案 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |