# Using conditions in ActiveRecord

**URL:** <https://www.ruby-forum.com/t/using-conditions-in-activerecord/52770>\
**Category:** Rails\
**Created:** [January 18, 2006, 10:56pm UTC](https://www.ruby-forum.com/t/using-conditions-in-activerecord/52770 "2006-01-18T22:56:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [January 18, 2006, 10:56pm UTC](https://www.ruby-forum.com/t/using-conditions-in-activerecord/52770/1 "2006-01-18T22:56:13Z")

</div>

This bit of code finds several groups; all those which have  
entity\_id’s of ‘1’ OR ‘2’ OR ‘3’, as you would expect:

ee = [1, 2, 3]  
groups = Group.find(:all, :conditions =\> “entity\_id in (” + ee.join  
(",") + “)”)

But this bit of code only finds groups which have entity\_id’s of ‘1’:

ee = [1, 2, 3]  
groups = Group.find(:all, :conditions =\> [ “entity\_id in (?), ee.join  
(”,") ] )

Why is that?

Evidently, if I switch the order like this:

ee = [3, 2, 1]

I only get groups with an entity\_id of ‘3’. So clearly if I specify  
the parameter like this ‘(?)’, I get only the first element from the  
‘ee’ list included in the where clause. Why?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [January 18, 2006, 11:02pm UTC](https://www.ruby-forum.com/t/using-conditions-in-activerecord/52770/2 "2006-01-18T23:02:16Z")

</div>

On Jan 18, 2006, at 1:16 PM, David A. wrote:

> groups = Group.find(:all, :conditions =\> [ "entity\_id in (?),  
> from the ‘ee’ list included in the where clause. Why?
> 
> Thanks in advance.

David-  
You can actually pass an array as the value to an IN query like this:

groups = Group.find(:all, :conditions =\> [ "entity\_id in (?),  
[1,2,3] ] )

Or

groups = Group.find(:all, :conditions =\> ["entity\_id in (?), ee] )

Cheers-  
-Ezra Z.  
Yakima Herald-Republic  
WebMaster  
[http://yakimaherald.com](http://yakimaherald.com)  
509-577-7732  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [January 18, 2006, 11:05pm UTC](https://www.ruby-forum.com/t/using-conditions-in-activerecord/52770/3 "2006-01-18T23:05:18Z")

</div>

2006/1/18, David A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> groups = Group.find(:all, :conditions =\> [ “entity\_id in (?), ee.join  
> (”,") ] )
> 
> Why is that?

In the second case, Rails “knows” that you are providing a string, so  
the resulting SQL will look like:

SELECT \* FROM groups WHERE entity\_id IN (‘1,2,3’)

Whereas in the first case, Rails knows it’s an array, and will do the  
right thing.

Hope that helps !

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [January 18, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/using-conditions-in-activerecord/52770/4 "2006-01-18T23:20:23Z")

</div>

Ezra Z. wrote:

> David-  
> You can actually pass an array as the value to an IN query like this:
> 
> groups = Group.find(:all, :conditions =\> ["entity\_id in (?), [1,2,3] ] )
> 
> Or
> 
> groups = Group.find(:all, :conditions =\> ["entity\_id in (?), ee] )

This is the method I always use to accomplish this:

groups = Group.find\_all\_by\_entity\_id([1, 2, 3])

-Brian

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [January 19, 2006, 12:25pm UTC](https://www.ruby-forum.com/t/using-conditions-in-activerecord/52770/5 "2006-01-19T12:25:15Z")

</div>

> groups = Group.find\_all\_by\_entity\_id([1, 2, 3])

Thanks for the suggestions. The plot thickens. If I do this:

ee = [4, 9]

Then the suggestions above both work:

groups = Group.find(:all, :conditions =\> [“entity\_id in (?)”, ee] )  
or  
groups = Group.find\_all\_by\_entity\_id(ee)

return what I would expect. However, ee is created like this:

ee = User.find(session[:user\_id]).entityadministrators.find(:all).map  
{ |e| [e.id]}

My understanding was that that would create an array of entity\_id’s.  
Indeed, it does seem to and if I join the array entries together with  
a comma and then use that result as a string condition in my groups  
query, it works. But if I just try to use the array as is, with the  
suggestions above, I get no rows returned.

So, the question becomes, what’s the difference between:

ee = User.find(session[:user\_id]).entityadministrators.find(:all).map  
{ |e| [e.id]} # which returns a 4 and and a 9

and

ee = [4, 9]

???

Thanks again.

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [January 19, 2006, 12:34pm UTC](https://www.ruby-forum.com/t/using-conditions-in-activerecord/52770/6 "2006-01-19T12:34:21Z")

</div>

David A. wrote:

> So, the question becomes, what’s the difference between:
> 
> ee = User.find(session[:user\_id]).entityadministrators.find(:all).map {  
> |e| [e.id]} # which returns a 4 and and a 9
> 
> and
> 
> ee = [4, 9]

Just a guess:

User.find(session[:user\_id]).entityadministrators.find(:all).map { |e|  
[e.id]} =\> [[4], [9]]

User.find(session[:user\_id]).entityadministrators.find(:all).map { |e|  
e.id} =\> [4,9]

?

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [January 19, 2006, 4:35pm UTC](https://www.ruby-forum.com/t/using-conditions-in-activerecord/52770/7 "2006-01-19T16:35:48Z")

</div>

> User.find(session[:user\_id]).entityadministrators.find(:all).map { |  
> e| e.id} =\> [4,9]

Right! This works (i.e. removing the extra square brackets in the  
first array creation):

@ee = User.find(session[:user\_id]).entityadministrators.find  
(:all, :order =\> “id asc”).map { |e| e.id}  
@groups = Group.find(:all, :conditions =\> ["entity\_id in (?) ", @ee])

Many thanks.
