11

我想查找基于多个参数的记录。但是这些参数有多个选项。

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 
4

1 回答 1

14

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

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 的 Active Record 指南:http: //guides.rubyonrails.org/active_record_querying.html

于 2012-04-17T10:40:40.693 回答