I've tried
@users = User.where(name: @request.requester or @request.regional_sales_mgr)
and
@users = User.where(name: @request.requester).where(name: @request.regional_sales_mgr).all
This doesn't seem to work. What I want is to find the user whose name matches @request.requester, and the user whose name matches @request.regional_sales_mgr, and save them both into the variable @users.
In the general case "OR" queries can be written as:
User.where("users.name = ? OR users.name = ?", request.requester, request.regional_sales_mgr)
Note: Rails 5 will support OR
using:
User.where(name: request.requester).or(User.where(name: request.regional_sales_mgr))
For this specific case as state in other answers an IN query is simpler:
User.where(name: [request.requester, request.regional_sales_mgr])
You want to use the SQL IN
clause. Activerecord provides a shortcut to this:
@users = User.where(name: [@request.requester, @request.regional_sales_mgr]).all
Giving an array of values to name:
will generate the following SQL statement:
SELECT * FROM users WHERE name IN (value1, value2, and so on...);
This should find all the users whose names are @request.requester
or @request.regional_sales_mgr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With