Forum: Ruby on Rails find in activerecord array

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
86a01abd9d77ebfc577b2890623c6170?d=identicon&s=25 bitterbal (Guest)
on 2007-03-25 20:14
(Received via mailing list)

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 ?

8310c5a7c769345114597bcdef111488?d=identicon&s=25 Ben Munat (Guest)
on 2007-03-25 22:23
(Received via mailing list)
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.

This topic is locked and can not be replied to.