Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find by multiple conditions in rails

I want to search a table with multiple conditions in Rails. I am using Active record and rails version 3.1.0.

I have Movies object, and want to achieve the equivalent of the following in rails:

Select * from Movies where rating = 'R' OR rating = 'PG'

I tried the following but it does not work

@filtered = Movies.find(:all, :conditions => { :rating => 'R', :rating => 'PG' })

Can you please provide help to write an equivalent of SQL query mentioned above.

like image 973
18bytes Avatar asked Aug 06 '12 14:08

18bytes


1 Answers

One way would be to build an "IN" condition with:

 @filtered = Movie.where(:rating => ['R', 'PG']).all

EDIT: I changed your class to "Movie" from "Movies". I assume that's what you will want.

like image 88
miked Avatar answered Oct 07 '22 12:10

miked