Find in activerecord array


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)

#<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

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

any tips ?


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| == 24710290 }

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