Ruby rails - 如何通过多个选项查找?

时间:2012-04-17 10:34:34

标签: ruby ruby-on-rails-3

我想根据多个参数找到记录。但这些参数是多种选择。

As "SELECT something FROM mytable WHERE user_name="xyz" and status=("Active" OR "Deleted")

如何将此转换为rails语句?

Person.find_by_user_name_and_status(user_name, status) # this doesn't take the OR operator 

1 个答案:

答案 0 :(得分:9)

我现在无法测试,但你试过吗?

Person.find_all_by_user_name_and_status(user_name, ["active", "deleted"])

如果以上不起作用,这应该......

Person.where(:user_name => "xyz", :status => ["active", "deleted"])
# translates to:
# "select * from persons where username = 'xyz' and status in ('active', 'deleted')"

您应该查看“活动记录的Rails指南”:http://guides.rubyonrails.org/active_record_querying.html