Noob question part 2 - Checking an object result is empty

26 views
Skip to first unread message

John Clegg

unread,
Oct 19, 2014, 10:36:54 PM10/19/14
to neo...@googlegroups.com
Hi

Using neo4j 3.0.1 + Ruby 2.0.0 + Sinatra + neo4j 2.1.2

What's the best way to check that a object result is empty?

eg 

movies = Movie.where(title: /.*#{request['q']}.*/i)



Let's say I want search for "fooobard" and I expect the result from neo4j to be empty.

How do I check this 

movies.empty?

John

Chris Grigg

unread,
Oct 19, 2014, 11:46:16 PM10/19/14
to neo...@googlegroups.com
Yup! movies.empty? or movies.blank? will both work and do the query server-side. There's an entry on these and some others at https://github.com/neo4jrb/neo4j/wiki/Optimized-Methods, you can do some cool with it to take the strain off of Ruby!

John Clegg

unread,
Oct 20, 2014, 3:20:22 AM10/20/14
to neo...@googlegroups.com
Hi 

I've figured out why I got confused.

If use 
movies = Movie.where(title: /.*#{request['q']}.*/i)

Then

movies.empty?

works.

But if I use "query_as" then it doesn't work. eg. (I know this is a very inefficient query but its to prove that it fails)

movies = Movie.query_as(:movies).match("(movies:Movie)").where("movie.title =~ '(?i)Matrix.*'").return("DISTINCT movie ORDER BY movie.title ASC")

movies.empty? fails

What should I use to test the movies object?

John

Chris Grigg

unread,
Oct 20, 2014, 5:22:32 AM10/20/14
to neo...@googlegroups.com
That query will always return true because `return` returns a struct that will respond to whatever columns you request from cypher. In that case, if you do `movies.movie.empty?`, you’ll get the result you’re looking for. Using `pluck` would be better because you don’t need to call the result by name. `Movie.query_as(:movies).match("(movies:Movie)").where("movie.title =~ '(?i)Matrix.*'”).pluck("DISTINCT movie ORDER BY movie.title ASC”).empty?`.

TL;DR use pluck in most cases. Read up on the difference between `pluck` and `return` and you’ll see they both have their uses.

If you’re just checking for number of returns, still isn’t the best way to do it because you’re performing the match, returning all results from the database, and counting in Ruby. You can do it server side like Neo4j::QueryProxy#count by doing it like this:

Movie.query_as(:movies).match("(movies:Movie)").where("movie.title =~ '(?i)Matrix.*'”).pluck("DISTINCT COUNT(movie) as total”).first == 0

The query returns an integer, `first` accesses it.
--
You received this message because you are subscribed to the Google Groups "neo4jrb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4jrb+u...@googlegroups.com.
To post to this group, send email to neo...@googlegroups.com.
Visit this group at http://groups.google.com/group/neo4jrb.
For more options, visit https://groups.google.com/d/optout.

John Clegg

unread,
Oct 20, 2014, 1:27:38 PM10/20/14
to neo...@googlegroups.com
Hi 

Thanks. This is what I need. Now my test will pass ! 

John
Reply all
Reply to author
Forward
0 new messages