Neo4j.rb creates a unique relationship

Here is my Neo4j Active Node

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

I want to create a unique relationship.

To avoid duplication, we should use create unique when creating a request for a cypher relationship.

Example:

MATCH (root { name: 'root' })
CREATE UNIQUE (root)-[:LOVES]-(someone)
RETURN someone

refer: http://neo4j.com/docs/stable/query-create-unique.html

How can I do this in Neo4j.rb with Rails ...?

Thanks in advance.

+4
source share
2 answers

We have a problem open to:

https://github.com/neo4jrb/neo4j/issues/473

Now I would suggest creating such a method in the model 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

EDIT : see mrstif answer for update

+3
source

, :

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
+5

Source: https://habr.com/ru/post/1570272/


All Articles