Rails where clause help

31 views
Skip to first unread message

Arup Rakshit

unread,
Feb 18, 2014, 8:41:11 AM2/18/14
to rubyonra...@googlegroups.com
Hi,

I found one answer from - http://stackoverflow.com/a/20198450/2767755

@users = User.where(name: [@request.requester,
@request.regional_sales_mgr]).all

Here @users will be having all those users, whose names are either
`@request.requester` or `@request.regional_sales_mgr`.

But what is the Ruby statement to get the result of -

"@users will be having all those users, whose names are
`@request.requester` and `@request.regional_sales_mgr`" ?

--
Posted via http://www.ruby-forum.com/.

Robert Walker

unread,
Feb 18, 2014, 9:10:29 AM2/18/14
to rubyonra...@googlegroups.com
Arup Rakshit wrote in post #1137044:
I don't see how you're request make sense. Here we have a single
attribute ("name") so you seem to be asking how to ask something like
the follow example:

Users where name is "Bob" and "Alice". How can a single attribute
simultaneously have two different values? It seems to me that you want
to use the OR condition just like the original statement, which would
cause you @users to contain all users where the name is either "Bob" or
"Alice".

But if you really want to see the syntax here's what it might look like:

@users.where({ name: @request.requester, name:
@request.regional_sales_mgr }).all

Just note that this would never return any results since "name" can only
have one value at a time.

Robert Walker

unread,
Feb 18, 2014, 9:13:23 AM2/18/14
to rubyonra...@googlegroups.com
Robert Walker wrote in post #1137051:
> @users.where({ name: @request.requester, name:
> @request.regional_sales_mgr }).all
>
> Just note that this would never return any results since "name" can only
> have one value at a time.

Except in the case where @request.requester ==
@request.regional_sales_mgr.

Arup Rakshit

unread,
Feb 18, 2014, 9:18:53 AM2/18/14
to rubyonra...@googlegroups.com
Robert Walker wrote in post #1137052:
> Robert Walker wrote in post #1137051:
>> @users.where({ name: @request.requester, name:
>> @request.regional_sales_mgr }).all
>>
>> Just note that this would never return any results since "name" can only
>> have one value at a time.

Sorry! for making you confused.

Look the below :

Buyer.joins(:products).where({'product.id' => [11,21,13]})

It will give us all buyers who bought a product having ID (11 or 21 or
13).

But I want those buyers who bought all products(i.e 11 and 21 and 13).

Hope now my question makes sense now. :-)
Reply all
Reply to author
Forward
0 new messages