Neo4j - cypher 和 Ruby gem 返回的结果出现意外差异
Neo4j - unexpected difference in the results returned by cypher and Ruby gem
我有两个节点,User 和 Thema(这是书籍的分类标准)。
这是我的 类 与此事有关的内容。
用户:
class User
include Neo4j::ActiveNode
property :name, type: String, constraint: :unique
property :created_at, type: DateTime
property :updated_at, type: DateTime
has_many :out, :languages, rel_class: :CommunicatingIn
has_many :out, :posts, rel_class: :PostedBy
has_many :out, :wit_dims, rel_class: :WitDimedBy
has_many :out, :themas, rel_class: :QualifiedFor
validates :name, presence: true
end
主题:
class Thema
include Neo4j::ActiveNode
property :code, type: String, constraint: :unique
property :subject, type: String, constraint: :unique
validates :code, presence: true
validates :subject, presence: true
has_many :out, :qualifiers, rel_class: :QualifiedBy
has_many :in, :users, rel_class: :QualifiedFor
end
合格对象:
class QualifiedFor
include Neo4j::ActiveRel
from_class :User
to_class :Thema
property :qualified_as, type: String, default: 'Reader'
validates :qualified_as, presence: true
validates_inclusion_of :qualified_as, :in => ['Academic', 'Author', 'Critic', 'Expert', 'Enthusiast', 'Publisher', 'Reader']
end
这是我在 rails 控制台中的 运行:
irb(main):021:0> u = User.create name: 'Levi'
CYPHER 724ms CREATE (n:`User` {props}) RETURN ID(n) | {:props=>{:uuid=>"432a58ca-1759-4b1c-92f9-0cd719140598", :name=>"Levi", :created_at=>1446056265, :updated_at=>1446056265}}
=> #<User uuid: "432a58ca-1759-4b1c-92f9-0cd719140598", created_at: Wed, 28 Oct 2015 18:17:45 +0000, name: "Levi", updated_at: Wed, 28 Oct 2015 18:17:45 +0000>
irb(main):022:0> t = Thema.find_by(code: 'AVLP')
CYPHER 126ms MATCH (n:`Thema`) WHERE (n.code = {n_code}) RETURN n LIMIT {limit_1} | {:n_code=>"AVLP", :limit_1=>1}
=> #<Thema uuid: nil, code: "AVLP", subject: "Popular music">
irb(main):023:0> t.users << u
CYPHER 122ms MATCH start, end WHERE (ID(start) = {ID_start}) AND (ID(end) = {ID_end}) CREATE start<-[rel1:`QUALIFIED_FOR`]-end | {:ID_start=>131, :ID_end=>1969}
=> #<QueryProxy Thema#users CYPHER: "MATCH thema131 WHERE (ID(thema131) = {ID_thema131}) MATCH thema131<-[rel1:`QUALIFIED_FOR`]-(result_users:`User`)">
irb(main):024:0> t.users.count
Thema#users 116ms MATCH thema131 WHERE (ID(thema131) = {ID_thema131}) MATCH thema131<-[rel1:`QUALIFIED_FOR`]-(result_users:`User`) RETURN count(result_users) AS result_users | {:ID_thema131=>131}
=> 1
irb(main):025:0> t.users.each_rel.first
Thema#users 121ms MATCH thema131 WHERE (ID(thema131) = {ID_thema131}) MATCH thema131<-[rel1:`QUALIFIED_FOR`]-(result_users:`User`) RETURN rel1 | {:ID_thema131=>131}
CYPHER 115ms MATCH n WHERE (ID(n) = {ID_n}) RETURN n | {:ID_n=>1969}
CYPHER 116ms MATCH n WHERE (ID(n) = {ID_n}) RETURN n | {:ID_n=>131}
=> #<QualifiedFor (:User {uuid: "432a58ca-1759-4b1c-92f9-0cd719140598"})-[:QUALIFIED_FOR]->(:Thema {uuid: nil}) qualified_as: "Reader">
irb(main):030:0* t.users.each_rel.first.qualified_as
=> "Reader"
哪个 returns 用户 "Levi" 有资格成为 Thema 的 "Reader"。
但是,当我尝试用密码找出哪个用户 "Levi" 符合条件时,我得到 "qualified as" 的 NULL,请参阅下面的查询:
neo4j-sh (?)$ match (u:User{name:'Levi'})-[r:QUALIFIED_FOR]-(t:Thema) return u.name, t.code, t.subject, r, r.qualified_as;
+----------------------------------------------------------------------------+
| u.name | t.code | t.subject | r | r.qualified_as |
+----------------------------------------------------------------------------+
| "Levi" | "AVLP" | "Popular music" | :QUALIFIED_FOR[608]{} | <null> |
+----------------------------------------------------------------------------+
1 row
34 ms
这里有什么问题?
是的,很抱歉!
不幸的是,当您执行 t.users << u
时,您的 ActiveRel
模型根本不参与。这是克里斯和我讨论过几次的事情。
目前确保此功能有效的最佳方法是:
QualifiedFor.create(from_node: u, to_node: t)
我有两个节点,User 和 Thema(这是书籍的分类标准)。
这是我的 类 与此事有关的内容。
用户:
class User
include Neo4j::ActiveNode
property :name, type: String, constraint: :unique
property :created_at, type: DateTime
property :updated_at, type: DateTime
has_many :out, :languages, rel_class: :CommunicatingIn
has_many :out, :posts, rel_class: :PostedBy
has_many :out, :wit_dims, rel_class: :WitDimedBy
has_many :out, :themas, rel_class: :QualifiedFor
validates :name, presence: true
end
主题:
class Thema
include Neo4j::ActiveNode
property :code, type: String, constraint: :unique
property :subject, type: String, constraint: :unique
validates :code, presence: true
validates :subject, presence: true
has_many :out, :qualifiers, rel_class: :QualifiedBy
has_many :in, :users, rel_class: :QualifiedFor
end
合格对象:
class QualifiedFor
include Neo4j::ActiveRel
from_class :User
to_class :Thema
property :qualified_as, type: String, default: 'Reader'
validates :qualified_as, presence: true
validates_inclusion_of :qualified_as, :in => ['Academic', 'Author', 'Critic', 'Expert', 'Enthusiast', 'Publisher', 'Reader']
end
这是我在 rails 控制台中的 运行:
irb(main):021:0> u = User.create name: 'Levi'
CYPHER 724ms CREATE (n:`User` {props}) RETURN ID(n) | {:props=>{:uuid=>"432a58ca-1759-4b1c-92f9-0cd719140598", :name=>"Levi", :created_at=>1446056265, :updated_at=>1446056265}}
=> #<User uuid: "432a58ca-1759-4b1c-92f9-0cd719140598", created_at: Wed, 28 Oct 2015 18:17:45 +0000, name: "Levi", updated_at: Wed, 28 Oct 2015 18:17:45 +0000>
irb(main):022:0> t = Thema.find_by(code: 'AVLP')
CYPHER 126ms MATCH (n:`Thema`) WHERE (n.code = {n_code}) RETURN n LIMIT {limit_1} | {:n_code=>"AVLP", :limit_1=>1}
=> #<Thema uuid: nil, code: "AVLP", subject: "Popular music">
irb(main):023:0> t.users << u
CYPHER 122ms MATCH start, end WHERE (ID(start) = {ID_start}) AND (ID(end) = {ID_end}) CREATE start<-[rel1:`QUALIFIED_FOR`]-end | {:ID_start=>131, :ID_end=>1969}
=> #<QueryProxy Thema#users CYPHER: "MATCH thema131 WHERE (ID(thema131) = {ID_thema131}) MATCH thema131<-[rel1:`QUALIFIED_FOR`]-(result_users:`User`)">
irb(main):024:0> t.users.count
Thema#users 116ms MATCH thema131 WHERE (ID(thema131) = {ID_thema131}) MATCH thema131<-[rel1:`QUALIFIED_FOR`]-(result_users:`User`) RETURN count(result_users) AS result_users | {:ID_thema131=>131}
=> 1
irb(main):025:0> t.users.each_rel.first
Thema#users 121ms MATCH thema131 WHERE (ID(thema131) = {ID_thema131}) MATCH thema131<-[rel1:`QUALIFIED_FOR`]-(result_users:`User`) RETURN rel1 | {:ID_thema131=>131}
CYPHER 115ms MATCH n WHERE (ID(n) = {ID_n}) RETURN n | {:ID_n=>1969}
CYPHER 116ms MATCH n WHERE (ID(n) = {ID_n}) RETURN n | {:ID_n=>131}
=> #<QualifiedFor (:User {uuid: "432a58ca-1759-4b1c-92f9-0cd719140598"})-[:QUALIFIED_FOR]->(:Thema {uuid: nil}) qualified_as: "Reader">
irb(main):030:0* t.users.each_rel.first.qualified_as
=> "Reader"
哪个 returns 用户 "Levi" 有资格成为 Thema 的 "Reader"。
但是,当我尝试用密码找出哪个用户 "Levi" 符合条件时,我得到 "qualified as" 的 NULL,请参阅下面的查询:
neo4j-sh (?)$ match (u:User{name:'Levi'})-[r:QUALIFIED_FOR]-(t:Thema) return u.name, t.code, t.subject, r, r.qualified_as;
+----------------------------------------------------------------------------+
| u.name | t.code | t.subject | r | r.qualified_as |
+----------------------------------------------------------------------------+
| "Levi" | "AVLP" | "Popular music" | :QUALIFIED_FOR[608]{} | <null> |
+----------------------------------------------------------------------------+
1 row
34 ms
这里有什么问题?
是的,很抱歉!
不幸的是,当您执行 t.users << u
时,您的 ActiveRel
模型根本不参与。这是克里斯和我讨论过几次的事情。
目前确保此功能有效的最佳方法是:
QualifiedFor.create(from_node: u, to_node: t)