开发者

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 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜