Dylan Richards
Dylan Richards

Reputation: 708

More than one ActiveRecord query at once?

Here's an action in my controller. What it currently does is display all Guides that have a least any spelling of the word 'abyss' in it. What I'd like to do is display all guides that have either any spelling of 'abyss' or any spelling of 'cat' in it as well.

I've tried all sorts of '%cat%' combinations, but can't find the right place to put it.

How do I make this work?

 def abyss
    t = Guide.arel_table
    @guides = Guide.where(t[:title].matches('%abyss%'))
  end

Upvotes: 0

Views: 72

Answers (2)

Utsav Kesharwani
Utsav Kesharwani

Reputation: 1745

You can use find_by_sql to simplify such queries:

def abyss
  @guides = Guide.find_by_sql(["select * from guides where title LIKE ? OR title LIKE ?", '%abyss%', '%cat%'])
end

Upvotes: 0

vee
vee

Reputation: 38645

Try with or as follows:

def abyss
  t = Guide.arel_table
  @guides = Guide.where(t[:title].matches('%abyss%').or(t[:title].matches('%cat%')))
end

Upvotes: 1

Related Questions