0

Say there are 2 models:

class A
  include Neo4j::ActiveNode
    property :name, type: String
    has_many :in, :bs, type: :HAS_B
end

class B
  include Neo4j::ActiveNode
    property :name, type: String
end

And following nodes and relations:

a1 <- b1
a2 <- b1
a3 <- b2
a1 <- b2

Now, I want all nodes of label: A that are in relation to a specific node of label: B.

How can I achieve that through neo4jrb?

In simple english, I want "All nodes labelled A which have a relation to node b1" (and this can be extended to multiple nodes, like all nodes of label A which have relation with nodes b1 and b2)

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
vish4071
  • 5,135
  • 4
  • 35
  • 65
  • Why would anyone downvote without commenting. Suggest an improvement if there needs to be...if it is unclear or something. – vish4071 May 12 '17 at 01:01

2 Answers2

0

This is how I did it:

A.as(:a).B.where(name: [b1])

Here, for multiple b's, just send array of all required b's.

Note that, this query gives all a's which are connected to any of the b's in the array, which suited my requirement in this case. If you want something that gives all a's which are connected to all b's, this won't work. However, if anyone comes across such query, post it in comments and I will include it here in this post.

vish4071
  • 5,135
  • 4
  • 35
  • 65
0

You should add the :bs association to class B.

class B
  include Neo4j::ActiveNode
    property :name, type: String
    has_many :out, :bs, type: :HAS_B, model_class: :A
end

Then, after finding a particular node B, you can simply do b.bs. For example B.where(id: some_id).bs.

If you want to find all nodes of label A which have relation with nodes b1 OR b2, you could do

A.all.branch { bs.as(:b).where("b.uuid IN [$b1_id, $b2_id]") }.params(b1_id: b1_id, b2_id: b2_id)

If you want to find all nodes of label A which have relation with nodes b1 AND b2, you could do

A.all.branch { bs.where(id: b1_id) }.branch { bs.where(id: b2_id) }
John
  • 9,249
  • 5
  • 44
  • 76