Neo4j gem - Detailed Querying direction with has_m

2019-07-24 12:47发布

问题:

I was able to do a specific pattern match from this original question Neo4j gem - Preferred method to deal with admin relationship via this

Event.query_as(:event).match("(event)<-[invite:invited]-(user1:User)<-[friends_with:friends_with]-(user2:User)").where('invite.admin = true').pluck(:event)

I was unable to get a modified version such as this to work

current_user.friends.events.query_as(:event).match("(event)<-[invite:invited]-(user:User)").where(invite: {admin: true}).pluck(:event)

So what I actually changed was I changed the direction for the from_node and to_node in my invite class

  from_class User
  to_class   Event
  type 'invited'

It was previously set as from Event to User. One of my questions is, why did I have to change that to make the query work? Doesn't the has_many: both mean that direction doesn't matter?

Another change was changing my relationship type to lower case. In my model it is written in all lowercase which does seem to matter. I thought both would be converted to all uppercase like the way neo4j does it but as it stands right now, it doesn't.

I do think I need to get the current_user method working as I only want the events of the current_user's friends. Suggestions?

回答1:

Okay, I was mixing up two different style of queries

For example in the docs they have this when you have already chained up to the lesson

s.lessons(:l, :r).where("r.start_date < {the_date} and r.end_date >= {the_date}").params(the_date: '2014-11-22').pluck(:l)

And something like this

Student.query_as(:s).where("s.age < {age} AND s.name = {name} AND s.home_town = {home_town}").params(age: params[:age], name: params[:name], home_town: params[:home_town]).pluck(:s)

Pay attention to where you place things like . and the query_as versus the lessons(:l, :r) part

My final query that worked eliminated any match components because I was already at the relationship and node that I was querying. So it looks something like this.

current_user.friends.events(:event, :rel).where("rel.admin = {admin_p} AND event.detail = {detail_p}").params(admin_p: true, detail_p: true).pluck(:event)

Would still like an answer to some of my questions above though!