# Using an arrray as a parameter value in ActiveRecord

**URL:** <https://www.ruby-forum.com/t/using-an-arrray-as-a-parameter-value-in-activerecord/115343>\
**Category:** Rails\
**Created:** [September 24, 2007, 7:09am UTC](https://www.ruby-forum.com/t/using-an-arrray-as-a-parameter-value-in-activerecord/115343 "2007-09-24T07:09:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [September 24, 2007, 7:09am UTC](https://www.ruby-forum.com/t/using-an-arrray-as-a-parameter-value-in-activerecord/115343/1 "2007-09-24T07:09:32Z")

</div>

Is it possible to use an array as a parameter value? I tried this and  
got a mysql error, “Operand should contain 1 column(s)”.

class User \< ActiveRecord::Base

has\_many :locations

def events  
location\_ids = []  
for location in self.locations  
location\_ids \<\< location.id  
end  
Event.find(:all, :conditions =\> [“location\_id = ?”, location\_ids])  
end

Thanks in advance.

Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [September 24, 2007, 7:29am UTC](https://www.ruby-forum.com/t/using-an-arrray-as-a-parameter-value-in-activerecord/115343/2 "2007-09-24T07:29:46Z")

</div>

If you are certain the values are save (as in your example), I have  
used this many times:

in = “(”+self.locations.collect{|loc| loc.id}.join(’,’)+")"  
Event.find(:all, :conditions =\> “location\_id in #{in}”)

As I said before, make sure you know the values are safe or there is an  
obvious sql injection, but for id’s coming from your database, this  
should work well.

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [September 24, 2007, 7:30am UTC](https://www.ruby-forum.com/t/using-an-arrray-as-a-parameter-value-in-activerecord/115343/3 "2007-09-24T07:30:46Z")

</div>

make that “values are _safe_”…I need to go to bed 🙂

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [September 24, 2007, 8:02am UTC](https://www.ruby-forum.com/t/using-an-arrray-as-a-parameter-value-in-activerecord/115343/4 "2007-09-24T08:02:40Z")

</div>

William P. wrote:

> in = “(”+self.locations.collect{|loc| loc.id}.join(’,’)+")"  
> Event.find(:all, :conditions =\> “location\_id in #{in}”)

Thanks William! Values will indeed always be coming in from the db, so  
this works great 🙂

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [September 24, 2007, 10:04am UTC](https://www.ruby-forum.com/t/using-an-arrray-as-a-parameter-value-in-activerecord/115343/5 "2007-09-24T10:04:16Z")

</div>

Peter M. wrote:

> Is it possible to use an array as a parameter value? I tried this and  
> got a mysql error, “Operand should contain 1 column(s)”.
> 
> class User \< ActiveRecord::Base
> 
> has\_many :locations
> 
> def events  
> location\_ids = []  
> for location in self.locations  
> location\_ids \<\< location.id  
> end  
> Event.find(:all, :conditions =\> [“location\_id = ?”, location\_ids])  
> end

What you need is:

Event.find(:all, :conditions =\> [“location\_id IN (?)”, location\_ids])

It’s better to do it that way even if you are certain that the values  
are coming from the database only. In this case it might be a bit  
contrived because you are also pretty sure that it are integers only,  
but generally unescaping may occur whilst reading from the database, and  
you will want to be sure stuff is properly escaped and quoted again.

–  
Roderick van Domburg  
[http://www.nedforce.com](http://www.nedforce.com)

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [September 25, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/using-an-arrray-as-a-parameter-value-in-activerecord/115343/6 "2007-09-25T23:03:06Z")

</div>

On 9/24/07, Roderick van Domburg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> What you need is:
> 
> Event.find(:all, :conditions =\> [“location\_id IN (?)”, location\_ids])

And even one more way to do it is:

Event.find :all, :conditions =\> { :location\_id =\> location\_ids}

which generates the same SQL

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [September 24, 2007, 10:16am UTC](https://www.ruby-forum.com/t/using-an-arrray-as-a-parameter-value-in-activerecord/115343/7 "2007-09-24T10:16:26Z")

</div>

> What you need is:
> 
> Event.find(:all, :conditions =\> [“location\_id IN (?)”, location\_ids])
> 
> It’s better to do it that way even if you are certain that the values  
> are coming from the database only. In this case it might be a bit  
> contrived because you are also pretty sure that it are integers only,  
> but generally unescaping may occur whilst reading from the database, and  
> you will want to be sure stuff is properly escaped and quoted again.
> 
> –  
> Roderick van Domburg  
> [http://www.nedforce.com](http://www.nedforce.com)

Thanks Roderick. That sounds reasonable. It will also make it more  
concise to do it this way as I will be putting in more requirements.
