# Find in activerecord array

**URL:** <https://www.ruby-forum.com/t/find-in-activerecord-array/90073>\
**Category:** Rails\
**Created:** [March 25, 2007, 8:14pm UTC](https://www.ruby-forum.com/t/find-in-activerecord-array/90073 "2007-03-25T20:14:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bitterbal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@bitterbal](https://www.ruby-forum.com/u/bitterbal)\
**Post date:** [March 25, 2007, 8:14pm UTC](https://www.ruby-forum.com/t/find-in-activerecord-array/90073/1 "2007-03-25T20:14:58Z")

</div>

Hi,

I have a table with products. I would like to run some searches on  
this table but do not want to query the sql database everytime. This  
causes too many select queries because I want to do many searches on a  
single page.

@products = Product.find(:all)

returns:  
#\<Product:0x33f8560 @attributes={“updated\_at”=\>nil, “label\_free”=\>"",  
“min\_stock”=\>“0”, “product\_id\_supplier”=\>“141499”, “max\_stock”=\>“0”,  
“id”=\>“24710289”, “description”=\>“PANNENLAP “, “orderunit”=\>””,  
“supplier\_id”=\>“32”, “description\_supplier”=\>"", “created\_at”=\>nil,  
“active”=\>“1”, “label\_description”=\>“PANNENLAP”}\>, #\<Product:0x33f8538  
@attributes={“updated\_at”=\>nil, “label\_free”=\>"", “min\_stock”=\>“0”,  
“product\_id\_supplier”=\>“129019883”, “max\_stock”=\>“0”,  
“id”=\>“24710290”, “description”=\>“LAMPVOET GROOT ROND “,  
“orderunit”=\>””, “supplier\_id”=\>“35”, “description\_supplier”=\>"",  
“created\_at”=\>nil, “active”=\>“1”, “label\_description”=\>“LAMPVOET GROOT  
ROND”}\>

Now I would like to search for “id=24710290” and return me the  
description.

any tips ?

thanks

---

<div class="post-metadata">

**Author:** ![bitterbal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@bitterbal](https://www.ruby-forum.com/u/bitterbal)\
**Post date:** [March 25, 2007, 10:23pm UTC](https://www.ruby-forum.com/t/find-in-activerecord-array/90073/2 "2007-03-25T22:23:12Z")

</div>

Array, having Enumerable mixed in, has a “find” method that takes a  
block and returns the first item that matches the condition in the block  
(or nil if none do).

So, you should be able to do:

product = @product.find { |p| p.id == 24710290 }

I’m assuming that id is a number there… put it in quotes if not.

b
