LDB
LDB

Reputation: 692

Neo4j - relationships not working after including type in ROR classes definition

I am getting and author but all work of arts belonging to the author are no longer retrieved.

> a = Author.find_by(author_name: 'Camus, Albert')
=> #<Author author_id: 615454, author_name: "Camus, Albert">

> w = a.wokas
=> <AssociationProxy @query_proxy=<QueryProxy Author#wokas#wokas CYPHER: "MATCH author615452, author615452-[rel1:`authored`]->(result_wokas:`Woka`) WHERE (ID(author615452) = {ID_author615452})">>

> w.count
=> 0

I should get like 300+ records.

In the DB the name of relationship is AUTHORED and the classes definition are:

class Author
  include Neo4j::ActiveNode
  property :author_name,  type: String
  property :author_id,    type: Integer
  has_many :out, :wokas, type: 'authored'
end

class Woka
  include Neo4j::ActiveNode
  property :author_id,    type: Integer
  property :publisher_id, type: Integer
  property :language_id,  type: Integer
  property :woka_id,      type: String #Integer
  property :woka_title,   type: String
  has_one :in, :author,     type: 'authored'
  has_one :in, :publisher,  type: 'published'
  has_one :in, :language,   type: 'used'
  has_many :out, :bisacs,       type: 'included'
  has_many :out, :descriptions, type: 'has_language'
end

Any clue why the relationships are not longer working?

Upvotes: 1

Views: 44

Answers (1)

Brian Underwood
Brian Underwood

Reputation: 10856

The associations return AssociationProxy objects now. In the past they returned QueryProxy objects. Both are there to allow you to make chained calls on further associations or other class-level methods. Like this:

# Returns another `AssociationProxy` which you can use to get all of the description objects
a.wokas.descriptions 

If you want to see the objects from the association you can call to_a on the result like this:

w = a.wokas.to_a

Or you could simply iterate since AssociationProxy objects are Enumerable:

a.wokas.each do |woka|
  # Do something with the woka object
end

As a side note, one of the reasons the AssociationProxy is there is to allow for eager loading as described here (again, that documentation won't be complete until the final release of 5.0).

Lastly for performance reasons I'd suggest using symbols whenever you can. For your associations, for example, you can do this:

has_many :out, :wokas, type: :authored

Upvotes: 1

Related Questions