Neo4j - cypher和Ruby gem返回的结果出乎意料的差异

时间:2015-10-28 18:32:57

标签: neo4j cypher neo4j.rb

我有两个节点,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

THEMA:

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

QualifiedFor:

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"

返回用户&#34; Levi&#34;合格为&#34;读者&#34;对于Thema。

然而,当我在密码中尝试找出什么用户&#34; Levi&#34;我有资格获得NULL,因为&#34;符合&#34;,请参阅下面的查询:

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

这里有什么问题?

1 个答案:

答案 0 :(得分:1)

是的,对不起!

不幸的是,当您执行t.users << u时,您的ActiveRel模型根本不参与。这是克里斯和我曾多次讨论过的事情。

目前,确保其有效的最佳方法是:

QualifiedFor.create(from_node: u, to_node: t)