1

I'm using Neo4jrb with Rails and I'm trying to exploit as much as possible the QueryProxy chaining methods to build complex query but I'm stuck when considering nodes with multiple labels due to inheritance. The following raw example can give you and idea:

class Person
  include ActiveGraph::Node
  property :name, type: String
  property :gender, type: String

  has_many :out, :socials, type: :HAS_ACCOUNT, model_class: 'Social', unique: true
end

class Social
  include ActiveGraph::Node
  # nothing relevant
end

class Facebook < Social
  # Inheriting from Social, the labels for Facebook nodes are [:Social, :Facebook]

  property :name, type: String
  # other properties
  
  has_many :out, :friends, type: :FRIENDS_WITH, model_class: 'Facebook', unique: true
end

class Instagram < Social
    # Inheriting from Social, the labels for Instagram nodes are [:Social, :Instagram]

  # some property and relationship
end

A simple working query is to get/query the name of the Facebook account of a Person:

Person.as(:p).socials.where(name: "Someone")

Which return the following cypher query:

Person#socials 
  MATCH (n:`Person`)
  MATCH (n)-[rel1:`HAS_ACCOUNT`]->(result_socials3:`Social`)
  WHERE (result_socials3.name = $result_socials3_name)
  RETURN result_socials3 | {:result_socials3_name=>"Someone"}

So it seems to be no errors when query properties of a derived class (Facebook) of the model_class specified in the Person relationship.

However, when I try to query relationship defined in Facebook, activegraph doesn't find it and returns the following error:

Person.as(:p).socials.friends
# OR
Person.as(:n).socials.branch { friendsĀ } 

# ERROR
`method_missing': undefined method `friends' for #<AssociationProxy Person#socials []> (NoMethodError)

Questions:

  • Why I can query on the properties of a derived class of the one specified on model_class in Person relationships, but I cannot query on its relationships
  • Is there a way to query on the derived class relationships? Or maybe change temporarily the scope?
  • Is there a way to specify the label of the ending node of an association? Like, when I query for Person.socials, can I defined ending labels other than 'Social' for the AssociationProxy Person#social?

I've already tried some method like "scoping", "branch" and "as_models" but as far as I know none of them works.

Note: The following cypher query works:

MATCH (p:Person)
MATCH (p)-[:HAS_ACCOUNT]->(s:Social)
MATCH (s)-[:FRIENDS_WITH]->(f)
RETURN p.name
Sbunzini
  • 542
  • 2
  • 8

0 Answers0