2012-04-17 7 views
7

Voglio trovare record basati su più di parametri. Ma quei parametri sono di mupti opzioni.Rotaie ruby ​​- come trovare più opzioni?

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

Come si traduce questo in estratto conto?

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

risposta

9

non posso provarlo in questo momento, ma hai provato questo?

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

se quanto sopra non funziona, questo dovrebbe ...

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

Si dovrebbe dare un'occhiata alla Guida Rails per Active Record: http://guides.rubyonrails.org/active_record_querying.html

+0

Grazie! ha funzionato, l'ho appena trovato _all_by invece di find_all. l'operatore OR formulato. 'Person.find_all_by_user_name_and_status (user_name, [" active "," deleted "])' –

+1

Il secondo esempio non funziona: Person.where (: user_name => "xyz"). Where (: status => ["active", "cancellato"]) –

Problemi correlati