开发者

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

开发者 https://www.devze.com 2023-01-29 01:26 出处:网络
Given: @products = Products.all # Do some code to choose a specific id of a record from @products chosen_product_id = 1

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

精彩评论

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