# Searching ActiveRecord sets

**URL:** <https://www.ruby-forum.com/t/searching-activerecord-sets/63973>\
**Category:** Rails\
**Created:** [June 20, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/searching-activerecord-sets/63973 "2006-06-20T22:17:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![marstoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@marstoni](https://www.ruby-forum.com/u/marstoni)\
**Post date:** [June 20, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/searching-activerecord-sets/63973/1 "2006-06-20T22:17:30Z")

</div>

Hey all,

I’m learning AR and I’m trying to find the best way to search an  
ActiveRecord set in order to make a conditional statement decision,  
something like .has\_value?(value) but for a multi-dimensional array like  
an AR set of like 100 rows.

I know I can do something like this:

results = Table.find\_by\_sql("…")

for result in results  
if result.has\_value?(“value”)  
do something…  
end  
end

But I’d rather not iterate through every row of results to find out. I  
think I read somewhere about being able to use the .find method to  
search AR hashes? Something like: results.find(…) but I’m not able to  
find more info on that.

I want to be able to have a conditional statement and only have it run  
if there is an attribute somewhere in the AR set that matches a given  
value. Thanks for any help and suggestions in advance.

---

<div class="post-metadata">

**Author:** ![marstoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@marstoni](https://www.ruby-forum.com/u/marstoni)\
**Post date:** [June 21, 2006, 5:16am UTC](https://www.ruby-forum.com/t/searching-activerecord-sets/63973/2 "2006-06-21T05:16:08Z")

</div>

results is just a Ruby array of AR objects. Have a look at what you  
can do with arrays in Ruby:

[http://www.ruby-doc.org/core/classes/Array.html](http://www.ruby-doc.org/core/classes/Array.html)

Methods like select, reject, collect, delete\_if may be useful for you.

What exactly are you trying to achieve… more specifics would be  
useful.

-Jonathan.

---

<div class="post-metadata">

**Author:** ![marstoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@marstoni](https://www.ruby-forum.com/u/marstoni)\
**Post date:** [June 21, 2006, 6:29am UTC](https://www.ruby-forum.com/t/searching-activerecord-sets/63973/3 "2006-06-21T06:29:51Z")

</div>

Yes,

Basically I want an easy way to search the AR set for a condition and do  
something if that condition is met, ANYWHERE in the AR result set.

But it seems methods like .has\_value?, .has\_key?, .include? etc are  
specific for single arrays and hashes and not a multi-dimensional  
array/hash such as an AR result set.

There might be a simple method I’m over looking though, but I guess  
thats what I’m looking for 🙂

So from my previous examples:

results = Table.find\_by\_sql("…")

for result in results  
if result.has\_value?(“value”)  
do something…  
end  
end

Instead I’d like to do something like this to save resources and not  
start an iteration into the for loop at all unless the condition is met:

results = Table.find\_by\_sql("…")

if results.has\_value?(“value”)  
do something…  
end

Though as I mentioned above, .has\_value? doesn’t seem to work on AR  
result sets, so I’m looking for something that does and works similarly  
Hope that made some sense. Thanks

---

<div class="post-metadata">

**Author:** ![marstoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@marstoni](https://www.ruby-forum.com/u/marstoni)\
**Post date:** [June 21, 2006, 7:21am UTC](https://www.ruby-forum.com/t/searching-activerecord-sets/63973/4 "2006-06-21T07:21:22Z")

</div>

Jonathan,

Great, thanks alot. I think thats what I’m looking for and the tip  
about you just having to iterate over results. Thanks.

Jonathan V. wrote:

> Have a look at any?
> 
> [module Enumerable - RDoc Documentation](http://www.ruby-doc.org/core/classes/Enumerable.html#M003125)
> 
> if results.any? { |r| … }
> 
> # do something
> 
> end
> 
> You’ll always have to iterate over results. There’s no other way to  
> look at all the values …
> 
> -Jonathan.

---

<div class="post-metadata">

**Author:** ![marstoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@marstoni](https://www.ruby-forum.com/u/marstoni)\
**Post date:** [June 21, 2006, 7:00am UTC](https://www.ruby-forum.com/t/searching-activerecord-sets/63973/5 "2006-06-21T07:00:02Z")

</div>

Have a look at any?

[http://www.ruby-doc.org/core/classes/Enumerable.html#M003125](http://www.ruby-doc.org/core/classes/Enumerable.html#M003125)

if results.any? { |r| … }

# do something

end

You’ll always have to iterate over results. There’s no other way to  
look at all the values …

-Jonathan.
