Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby rails - how to find_by multiple options?

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?

Person.find_by_user_name_and_status(user_name, status) # this doesn't take the OR operator 
like image 624
Majoris Avatar asked Apr 17 '12 10:04

Majoris


1 Answers

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

like image 81
Simon Woker Avatar answered Sep 28 '22 10:09

Simon Woker