我正在将 Neo4jrb 与 Rails 一起使用,并且我正在尝试尽可能多地利用 QueryProxy 链接方法来构建复杂的查询,但是在考虑具有多个标签的节点时由于继承而被卡住了。以下原始示例可以给您和想法:
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
一个简单的工作查询是获取/查询一个人的 Facebook 帐户的名称:
Person.as(:p).socials.where(name: "Someone")
返回以下密码查询:
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"}
因此,查询Person 关系中指定的 model_class 的派生类(Facebook)的属性时似乎没有错误。
但是,当我尝试查询Facebook 中定义的关系时,activegraph 找不到它并返回以下错误:
Person.as(:p).socials.friends
# OR
Person.as(:n).socials.branch { friends }
# ERROR
`method_missing': undefined method `friends' for #<AssociationProxy Person#socials []> (NoMethodError)
问题:
- 为什么我可以查询Person 关系中model_class上指定的派生类的属性,但无法查询其关系
- 有没有办法查询派生类关系?或者可能暂时改变范围?
- 有没有办法指定关联结束节点的标签?比如,当我查询Person.socials时,我可以为 AssociationProxy Person#social 定义除“社交”之外的结束标签吗?
我已经尝试过一些方法,如“范围”、“分支”和“as_models”,但据我所知,它们都不起作用。
注意:以下密码查询有效:
MATCH (p:Person)
MATCH (p)-[:HAS_ACCOUNT]->(s:Social)
MATCH (s)-[:FRIENDS_WITH]->(f)
RETURN p.name