开发者

Make an array of id's of a model in Rails

开发者 https://www.devze.com 2023-03-10 01:44 出处:网络
My question is simple... or so it seems. If @set.cards retrieves \"id\", \"front\", \"back\", \"created_at\" (etc.). How d开发者_运维问答o I get just an array of the id\'s? [1,2,3,4,5...etc]?Rails pro

My question is simple... or so it seems. If @set.cards retrieves "id", "front", "back", "created_at" (etc.). How d开发者_运维问答o I get just an array of the id's? [1,2,3,4,5...etc]?


Rails provides a standard method for this:

@set.card_ids

Reference: The has_many documentation. Look at the 5th method from the top, i.e. collection_singular_ids.


This will work:

@set.cards.map(&:id)
0

精彩评论

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