2

Following the nobrainer docs here: http://nobrainer.io/docs/querying/

I am trying to do an OR query, to look for articles where a keyword matches the title or text fields...

The following query returns no results...

 s = 'something'
 @articles = Article.where(:or=>[:title=>/(?i)#{s}/, :text=>/(?i)#{s}/])

I checked my regex with the following query and it works...

 @articles = Article.where(:title=>/(?i)#{s}/)

Could someone help us understand how to do OR queries? Thanks.

UPDATE:

The query returns results only if the search term is found in both fields... so it appears to be working like an AND instead of an OR!

vanboom
  • 1,274
  • 12
  • 20

1 Answers1

1

The nobrainer documentation warns of this behaviour:

:or => [p1,...,pN] evaluates to true when at least one of the predicates is true.

Be aware that [:a => 1, :b => 2] is the same as [{:a => 1, :b => 2}], which is not the same as [{:a => 1}, {:b => 2}].

Your query may work if you add some curly brackets:

 @articles = Article.where(:or=>[{:title=>/(?i)#{s}/}, {:text=>/(?i)#{s}/}])
Etienne Laurin
  • 6,731
  • 2
  • 27
  • 31