通过Rails中的多个条件查找 [英] Find by multiple conditions in rails

查看:86
本文介绍了通过Rails中的多个条件查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Rails中搜索具有多个条件的表。我正在使用Active Record and Rails版本3.1.0。

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

我有Movies对象,并且想要实现以下效果:

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' })

请提供帮助以编写与上述SQL查询等效的方法。

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

推荐答案

一种方法是使用:

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

编辑:我将您的课程从电影更改为电影。我想这就是您想要的。

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

这篇关于通过Rails中的多个条件查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆