2

I have two nodes, User and Thema (this a categorizing standard for books).

Here are my classes related to this matter.

User:

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

Here is what I am running in the rails console:

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"

Which returns user "Levi" qualified as "Reader" for the Thema.

However, when I trying in cypher to find out what user "Levi" is qualified for I am getting a NULL for "qualified as", see the query below:

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

What is wrong here?

LDB
  • 692
  • 6
  • 18

1 Answers1

1

Yeah, sorry about that!

Unfortunately when you do t.users << u then your ActiveRel model isn't involved at all. This is something that Chris and I have discussed a couple of times.

For now the best way to make sure this works is to do:

QualifiedFor.create(from_node: u, to_node: t)
Brian Underwood
  • 10,746
  • 1
  • 22
  • 34
  • Just edited the solution telling why for the moment this is not working. – LDB Oct 29 '15 at 15:12
  • Worked when added the new packages provided: graphaware-server-all-2.3.0.35.jar and graphaware-uuid-2.3.0.35.7.jar for Neo4j database version 2.3.0. – LDB Oct 29 '15 at 17:20
  • FYI the master branch of the `neo4j` gem changes the behavior to use `ActiveRel` if specified as the `rel_type` of the association. It should come out with `6.0.0` – Brian Underwood Nov 04 '15 at 02:48