开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜