1

In my previous example I had this query

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)

current_user.friends.events brings me up to events to continue my chain, but does this work for if I already have an object that contains an array of events?

In my example, I am trying to use geocoder to pull in proximity.

So I have my user and events which are geocoded.

Geocoder can do something like this

Venue.near([40.71, 100.23], 20) # find all objs within 20 miles

So I can essentially find all the events and store it in an object. Now can I use the neo4j query to filter that object?

e.g.

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

Even if this works, is there an alternate way to do this?

Clam
  • 935
  • 1
  • 12
  • 24

1 Answers1

1

You can't call query proxy methods on an array, so just grab the IDs of those events and filter your match accordingly.

current_user.friends.events(:e).where(neo_id: array_object.map { |e| e.neo_id })

That'll filter the user's friends' events that are in that array. We can use neo_id in where for better performance.

subvertallchris
  • 5,282
  • 2
  • 25
  • 43
  • can I continue this chain with cypher queries or is it like 1 or the other? e.g. `current_user.friends.events(:e,:rel).where(neo_id: array_object.map { |e| e.neo_id }, "rel.admin = {admin_p} AND event.detail = {detail_p}").params(admin_p: true, detail_p: true))` This just looks like a mess to me but I can't test it until I fix my login issue (latest question) – Clam Nov 27 '14 at 23:36
  • You can do whatever you want with it, it's a QueryProxy object. If you start using the master branch, you can write it like `current_user.friends.events.where(neo_id: array_object.map { |e| e.neo_id }, detail: true).rel_where(admin: true)`. Not nearly as messy. – subvertallchris Nov 28 '14 at 00:38
  • I couldn't get it to work with neo_id but I did it with uuid. neo_id gives me `undefined method 'to_i' for [14, 15, 17]:Array` to_i is inside the method map? what's the diff between neo_id and uuid? – Clam Nov 29 '14 at 06:05
  • Oh, that's right, I ran into that before and then got sidetracked when I went to fix it. `neo_id` isn't a real property. When the gem sees you use that as a key, it formats the Cypher differently ("ID(n) = whatever" instead of "n.uuid = whatever") and calls `to_i` to ensure you didn't pass a string. It needs to be updated to say, "if value is an array, use `ID(n) IN #{value}`." It's in neo4j-core if you're feeling brave. ;-) – subvertallchris Nov 29 '14 at 06:32
  • Not that brave yet =p. Without `rel_where`, is this my only option to deal with a mix of params and both the node and rel? `current_user.friends.events(:e, :rel).where("e.uuid = {event_id} AND e.property = {property_p} AND rel.admin = {admin_p}").params(event_id: event_id, admin_p: true, property_p: 'Interested')` I don't think it's getting the proper array of ids from `event_id` though – Clam Nov 29 '14 at 07:12
  • If `event_id` is an array, use IN instead of =. But you can do that all differently. If you're pointing at the most recent node in the chain, use hashes instead of a string and it will create params and use IN for you if you give it an array. You only need to use a string if you need to provide an identifier, which you only need for the rel. You'll need to list your string first. `current_user.friends.events(:e, :rel).where('rel.admin = {admin_p}', uuid: event_id, property: 'Interested').params(admin_p: true)` – subvertallchris Nov 29 '14 at 19:41