mhaseeb
mhaseeb

Reputation: 1779

query_as using Neo4j 5.2.3 giving nil Class

I am using neo4j 5.2.3. I wrote a following query in a certain method in my model action.rb

def points_ranking
  Action.query_as(:a).match('(ag:Agenda)-[a:ACTION]->(u:User)').where("a.action_type = {action_type} and a.action_allowed = {action_allowed}").params(action_type: Action::TYPE[:stance_change] , action_allowed: true ).pluck('a')
end

The following is my action.rb

class Action 
  include Neo4j::ActiveRel
  property :action_type
  property :created_at,           type: DateTime
  property :updated_at,           type: DateTime
  property :deadline,             type: DateTime
  property :completed_at,         type: DateTime
  property :read_at,              type: DateTime
  property :expired_at,           type: DateTime
  ##other attributes and above method
end

This gives me the following error: Undefined method query_as for nil:nilClass Action #00078eb. Why is my Action class nil? If I perform the same query on Agenda model it returns me with results.

What am I doing wrong? Any help will be much appreciated.

Upvotes: 1

Views: 47

Answers (1)

Brian Underwood
Brian Underwood

Reputation: 10856

I'm not sure why Action is nil. I would first suggest making points_ranking a class method and using self instead of Action.

That said, the query_as method isn't defined on ActiveRel models. Now that you've tried it, I can see how it would be quite useful. I've made an issue to address this and to discuss it:

https://github.com/neo4jrb/neo4j/issues/1081

If it did exist, I would think it would define the match for you, matching on the from/to nodes as well as specifying IDs for them.

For your points_ranking method, I might implement it like this:

class Agenda
  include Neo4j::ActiveNode

  has_many :out, :action_users, rel_class: :Action
end

class Action
  def points_ranking
    from_node.action_users(:u, :a)
      .where_rel(action_type: Action::TYPE[:stance_change], action_allowed: true)
      .pluck(:a)
  end
end

Upvotes: 1

Related Questions