bswinnerton
bswinnerton

Reputation: 4721

Using has_many "both" in neo4j.rb

I'm looking for a way to set up a relationship between Users where you can use in, out, and both all at the same time in Neo4j.rb.

Here's what I have so far:

class User
  include Neo4j::ActiveNode

  has_many :both, :friends, type: :connection, model_class: User
  has_many :out, :following, type: :connection, model_class: User
  has_many :in, :followers, type: :connection, model_class: User
end

The following works:

me = User.create
you = User.create

me.followers << you
me.followers.to_a
#=> [you]

you.following.to_a
#=> [me]

The opposite of above works as well. But this doesn't seem to work:

me.friends << you
you.following.to_a
#=> []

Or:

me.followers.to_a
#=> []

However, this does:

me.following.to_a
#=> [you]

Upvotes: 3

Views: 415

Answers (1)

Brian Underwood
Brian Underwood

Reputation: 10856

This is expected behavior. Neo4j doesn't allow you to create relationships which don't have a direction. Thus, the both association type is only for querying (that is, when querying it specifies the relationship, but not the direction to/from the node).

Since Neo4j relationships always have a direction, when you create relationships with a both association it creates them as out relationships. See this section in the docs:

https://github.com/neo4jrb/neo4j/wiki/Neo4j-v3-Declared-Relationships#all-has_manyhas_one-method-calls-begin-with-declaration-of-direction

Thinking about it now, I'm wondering if perhaps Neo4j.rb shouldn't let you create relationships using both associations. What do you think? I'll create a Github issue as well

Upvotes: 4

Related Questions