# Can you run a find on an existing hash?

**URL:** <https://www.ruby-forum.com/t/can-you-run-a-find-on-an-existing-hash/166249>\
**Category:** Rails\
**Created:** [April 23, 2009, 7:10am UTC](https://www.ruby-forum.com/t/can-you-run-a-find-on-an-existing-hash/166249 "2009-04-23T07:10:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [April 23, 2009, 7:10am UTC](https://www.ruby-forum.com/t/can-you-run-a-find-on-an-existing-hash/166249/1 "2009-04-23T07:10:39Z")

</div>

This is probably a basic question, but I was wondering if / how it’s  
possible to run a find on an existing hash.

For instance:

1. I need to pull all products in the database for a simple list:  
@products = Products.find(:all)

2. At a separate portion of the page, I want to display products that  
are in, say, category 5.

From scratch, I could run…  
@new\_products = Products.find(:all, :conditions =\> {:category\_id =\> 5})

Although, I thought you could also do

@new\_products = @products.find(:all, :conditions =\> {:category\_id = 2})

My query has been returning a wrong number of arguments (2 for 1), so I  
wasn’t sure if I was headed on the right track.

---

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [April 23, 2009, 9:11am UTC](https://www.ruby-forum.com/t/can-you-run-a-find-on-an-existing-hash/166249/2 "2009-04-23T09:11:40Z")

</div>

On Apr 23, 6:10 am, Robert S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> From scratch, I could run…  
> @new\_products = Products.find(:all, :conditions =\> {:category\_id =\> 5})
> 
> Although, I thought you could also do
> 
> @new\_products = @products.find(:all, :conditions =\> {:category\_id = 2})
> 
> My query has been returning a wrong number of arguments (2 for 1), so I  
> wasn’t sure if I was headed on the right track.

Enumerables (and thus arrays) have a method called find, but it’s not  
an activerecord find and so has different syntax. Check the ruby  
standard library api for details of enumerable’s find.

Fred

---

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [April 23, 2009, 1:26pm UTC](https://www.ruby-forum.com/t/can-you-run-a-find-on-an-existing-hash/166249/3 "2009-04-23T13:26:20Z")

</div>

In the Enumerable’s find\_all method will be:

@products.find\_all { |product| product.category\_id = 2 }

This method returns an array, the find method returns only the first  
match. This approach is not the best if you have a lot of products, in  
that case you will use the ActiveRecord’s find and let the database do  
this work in a more efficient way.

Regards.

Franco C…

On Apr 23, 2:10 am, Robert S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
