0

I want to filter users by email, name and/or phone.

I have this in my usuarios_controller.rb:

if params[:usuario]
  @usuarios = Usuario.where({:Email => params[:usuario] }).order('created_at DESC')
end

How do I add a "OR" to this?

I want an OR condition among all these parameters.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
  • Haven't used MongoDB for awhile but `order('created_at desc')` looks like ActiveRecord/SQL to me but your question is tagged `mongodb`. Is this ActiveRecord or Mongoid/MongoMapper? – mu is too short Dec 21 '16 at 20:34

1 Answers1

0

You can use OR in where method like following:

Usuario.where("email = ? OR name = ? or phone = ?", email_value, name_value, phone_value)

It will build and execute the following SQL query:

SELECT "usuarios".* FROM "usuarios" WHERE (email = 'email_value' OR name = 'name_value' or phone = 'phone_value')
Arslan Ali
  • 17,418
  • 8
  • 58
  • 76