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)
精彩评论