Rails: Selecting a record in an object, given the object's ID

Given:

@products = Products.all

# Do some code to choose a specific id of a record from @products
chosen_product_id = 1

Current code makes a second call:

Product.find(chosen_product_id)

... which is inefficient. How do I select the correct record from the existing object without making another database call?

Answers


@products.detect {|p| p.id == chosen_product_id }

Need Your Help

Optimizing repeatedly called math function

performance optimization f#

I am writing some code as part of a nonlinear regression tool and I am trying to figure out an approach for returning the nth partial derivative of a given function in a way that is a good balance of