rails how to show an array in the view with has_and_belongs_to_many
I have Flowers and colors. I have set up a has_and_belongs_to_many relationship.
Through the console I can do:
q=Flower.first
q.colors
=> [#<Color id: 1, name: "Red", hex_code: "#FF0000", created_at: "2011-10-01 19:59:26", updated_at: "2011-10-01 19:59:26">, #<Color id: 3, name: "Blue", hex_code: "#0000FF", created_at: "2011-10-01 19:59:26", update开发者_如何学Pythond_at: "2011-10-01 19:59:26">]
and also:
q.color_ids
=> [1, 3]
How can I return the return color names? For example: ["Red", "Blue"]
.
This will give you an array of the color names:
q.colors.map(&:name)
精彩评论