1

I am trying to retrieve all BISAC nodes having the word "Art" in the description.

ba = Bisac.where(bisac_value =~ '.*Art.*')
NameError: undefined local variable or method `bisac_value' for main:Object

The equivalent cypher query retrieves 10 nodes.

MATCH (b:Bisac) WHERE (b.bisac_value =~ '.*Art .*') RETURN b;

What I am doing wrong here?

LDB
  • 692
  • 6
  • 18

3 Answers3

1

Your solution will certainly work, but an easier one which doesn't resort to using the Query API is to simply use a Ruby regular expression:

Bisac.all(:l).where(bisac_value: /.*Art.*/)

You can even use a case-insensitive regular expression (/.*Art.*/i) which will get translated into Cypher syntax as well.

Brian Underwood
  • 10,746
  • 1
  • 22
  • 34
  • Also, keep an eye out for new Neo4j.rb screencasts on the Neo4j blog tomorrow. The sixth episode addresses this, among other thing. – Brian Underwood Aug 17 '15 at 20:43
  • Is there a way to replace pluck with return? Can't make the version with return to work. – LDB Aug 17 '15 at 20:55
  • Ah, right, I didn't notice you were `pluck`ing `l`. You should be able to `return(:l).exec`, though I think you'll need to do a `query` call first, so like `query.return(:l).exec`. That will give you an object which you can enumerate over and each enumeration will give you a result object which you can call (in this case) the `l` method on – Brian Underwood Aug 17 '15 at 21:23
  • It is ending in error: ba = Bisac.all(:l).where(bisac_value: /.*Art.*/).return(:l).exec NoMethodError: undefined method `return' for # – LDB Aug 17 '15 at 21:44
  • Yeah, like I said you need to start with a call to `query` – Brian Underwood Aug 17 '15 at 21:47
  • Don't know how to do this. – LDB Aug 17 '15 at 22:03
  • So you would just do something like `Bisac.all(:l).where(bisac_value: /.*Art.*/).query.return(:l).each {|result| puts result.l.inspect }`. I take back the need for an `exec` as `Query` object are enumerable by themselves (as long as you're got a `return` in there) – Brian Underwood Aug 18 '15 at 15:13
  • 1
    Got it and worked perfect. I wasn't aware that I should use "query" in the construction. Many thanks for the hint. – LDB Aug 18 '15 at 16:43
  • Screencast #6 talks about the difference between proxy objects and query objects some. https://www.youtube.com/playlist?list=PL5klM3mD6alLUhNTPTbj5a3GBjU7oZN0t There's also this: https://www.youtube.com/playlist?list=PL5klM3mD6alLUhNTPTbj5a3GBjU7oZN0t – Brian Underwood Aug 18 '15 at 17:58
  • Excellent screen cast! – LDB Aug 18 '15 at 18:21
0

Found the answer (in the documentation), here is the link: http://neo4jrb.readthedocs.org/en/5.1.x/Querying.html

The query should be:

ba = Bisac.all(:l).where("l.bisac_value =~ {the_value}").params(the_value: '.*Art.*').pluck(:l)

or, much simpler:

ba = Bisac.all(:l).where("l.bisac_value =~ '.*Art.*'").pluck(:l)
LDB
  • 692
  • 6
  • 18
0

Removed Kaminari from pagination and used will_paginate. Used page_entries_info

Problem solved.

LDB
  • 692
  • 6
  • 18