result = music.users(:user)
.optional(:friends, :friend)
.where(neo_id: current_user.neo_id)
.pluck(:user, is_friend: 'friend IS NOT NULL')
This would return an array of tuples: Each user from the association, and a boolean stating if they are friends of the current user
The .optional(:friends, :friend)
is like calling .friends(:friend)
, but is uses an OPTIONAL MATCH
instead of a MATCH
in cypher for querying the association.
For the is_friend
part, that translates to RETURN friend IS NOT NULL AS is_friend
. Actually, since this is a pluck
, the AS
isn't necessary, so it could just be .pluck(:user, 'friend IS NOT NULL')
. Since it's doing an OPTIONAL MATCH
, the friend
variable in the query (which is the current_user
) will only be there if the user
from the result row is friends with the current_user
.
One thing that you can do to help with understanding of what the query chain would do is to call to_cypher
to see what query would be generated. In this case, that would be:
puts music.users(:user)
.optional(:friends, :friend)
.where(neo_id: current_user.neo_id)
.return(:user, is_friend: 'friend IS NOT NULL')
.to_cypher
The pluck
is a method that returns an array instead of a query chain object, so I replaced it with return
in this case.