# Search on variable-length array of values?

**URL:** <https://www.ruby-forum.com/t/search-on-variable-length-array-of-values/104224>\
**Category:** Rails\
**Created:** [May 15, 2007, 5:28pm UTC](https://www.ruby-forum.com/t/search-on-variable-length-array-of-values/104224 "2007-05-15T17:28:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [May 15, 2007, 5:28pm UTC](https://www.ruby-forum.com/t/search-on-variable-length-array-of-values/104224/1 "2007-05-15T17:28:30Z")

</div>

I am a bit stuck with the following problem, and would welcome  
suggestions.

I have two tables, e.g. “things” and “properties”, with one line of of  
things corresponding to one line of properties, and vice-versa, and 70  
elements per line of properties. Users want to be able to find all  
things where their favourite elements in the properties table are not  
null, and the number of things they are interested in is variable. I  
have put the list of elements on a page where users may select those  
they are interested in and this returns a hash containing only those  
they have selected, with values set to 1.

The only method I can come up with is to construct a string along the  
lines of:

“select thing\_id from properties where A is not null and B is not null  
and C is not null…”

…etc., then use this with Property.find\_by\_sql. Then, I’d have to use  
the output of that to do another search on things.

Is there any better way to do this with find? E.g something like:

@things = thing.find(:All, :conditions=\>[“properties.? is not null”,  
array\_of\_interesting\_elements], :include =\> :property)

This doesn’t work, of course, but perhaps there is something similar  
that might?

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [May 15, 2007, 5:52pm UTC](https://www.ruby-forum.com/t/search-on-variable-length-array-of-values/104224/2 "2007-05-15T17:52:15Z")

</div>

how about

@things = Thing.find\_by\_sql(“select t.\* from things t, properties p  
where #{@long\_not\_null\_string} t.id = p.id”)

that might work…

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [May 15, 2007, 5:56pm UTC](https://www.ruby-forum.com/t/search-on-variable-length-array-of-values/104224/3 "2007-05-15T17:56:31Z")

</div>

meep wrote:

> @things = Thing.find\_by\_sql(“select t.\* from things t, properties p  
> where #{@long\_not\_null\_string} t.id = p.id”)

D’oh! 😊  
You’re right - that works perfectly. Many thanks.
