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 data开发者_JAVA百科base call?
@products.detect {|p| p.id == chosen_product_id }
精彩评论