# Expanding an array parameter?

**URL:** <https://www.ruby-forum.com/t/expanding-an-array-parameter/68745>\
**Category:** Rails\
**Created:** [September 15, 2006, 2:06am UTC](https://www.ruby-forum.com/t/expanding-an-array-parameter/68745 "2006-09-15T02:06:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 15, 2006, 2:06am UTC](https://www.ruby-forum.com/t/expanding-an-array-parameter/68745/1 "2006-09-15T02:06:51Z")

</div>

Hi everyone,

I am wondering if there is a way to pass a function an array, but have  
it treated as if it was expanded.

Here is an example of what I am trying to do:

query\_string = “param1 = ? AND param2 = ?”  
parameters = [1,“test”]

Thing.find :all, :conditions =\> [query\_string, parameters]

Is there a way to do something like this? A way to have the parameters  
variable treated like an expanded array?

Thank you for your time,

JD

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 15, 2006, 2:26am UTC](https://www.ruby-forum.com/t/expanding-an-array-parameter/68745/2 "2006-09-15T02:26:52Z")

</div>

\<…\>

> Thing.find :all, :conditions =\> [query\_string, parameters]
> 
> Is there a way to do something like this? A way to have the parameters  
> variable treated like an expanded array?  
> \<…\>

You can expand array to values using \* (splat).

Try: Thing.find :all, :conditions =\> [query\_string, \*parameters]

## Regards, Rimantas

[http://rimantas.com/](http://rimantas.com/)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 15, 2006, 2:47am UTC](https://www.ruby-forum.com/t/expanding-an-array-parameter/68745/3 "2006-09-15T02:47:05Z")

</div>

Jackdan wrote:

> query\_string = “param1 = ? AND param2 = ?”  
> parameters = [1,“test”]
> 
> Thing.find :all, :conditions =\> [query\_string, parameters]

The second value of the conditions array should either be a hash or some  
replacement parameter value\*\*. What you have right now has an array:

[“param1 = ? AND param2 = ?”, [1,“test”]]

but what you really want is:

[“param1 = ? AND param2 = ?”, 1, “test”]

which can be achieved by adding two arrays:

:conditions =\> [query\_string] + [parameters]

You could alternatively build a hash with named parameters:

query\_string = “param1 = :p1 AND param2 = :p2”  
parameters = {:p1 =\> 1, :p2 =\> “test”}

:conditions =\> [query\_string, parameters]

\*\*Side Note: The second and subsequent values in the conditions array  
can be arrays which can be used like so:

Thing.find :all, :conditions =\> [‘id in(?)’, [1,2,3,4]]

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 15, 2006, 2:49am UTC](https://www.ruby-forum.com/t/expanding-an-array-parameter/68745/4 "2006-09-15T02:49:10Z")

</div>

Rimantas L. wrote:

> You can expand array to values using \* (splat).

Lovely. There’s always an easier way! Thanks, Rimantas.
