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

时间:2022-08-27 00:09:39

I want to find records based on more than parameters. But those parameters are of mupltiple options.

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

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

How do I translate this to rails statement?

如何将其转换为rails语句?

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

1 个解决方案

#1


9  

I can't test it right now, but did you try this?

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

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

if the above does not work, this should...

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

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

You should take a look into the Rails Guide for Active Record: http://guides.rubyonrails.org/active_record_querying.html

您应该查看Rails热门记录指南:http://guides.rubyonrails.org/active_record_querying.html

#1


9  

I can't test it right now, but did you try this?

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

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

if the above does not work, this should...

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

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

You should take a look into the Rails Guide for Active Record: http://guides.rubyonrails.org/active_record_querying.html

您应该查看Rails热门记录指南:http://guides.rubyonrails.org/active_record_querying.html