【问题标题】:Neo4j.rb create unique relationshipNeo4j.rb 创建唯一关系
【发布时间】:2015-01-07 09:57:21
【问题描述】:

这是我的 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

我怎样才能在 Neo4j.rb 中使用 Rails...?

提前谢谢..

【问题讨论】:

    标签: ruby-on-rails ruby neo4j neo4j.rb nosql


    【解决方案1】:

    作为更新,您现在可以执行以下操作:

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

    【讨论】:

      【解决方案2】:

      这是我们要解决的问题:

      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 的回答以获取更新

      【讨论】:

      • 刚刚提交了 PR 以将其添加到 gem 中。查看github.com/neo4jrb/neo4j/pull/660。除非布赖恩指出错误,否则我会在接下来的几个小时内合并它。 ;-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多