我有鲜花和颜色。我已经建立了一个has_and_belongs_to_many关系。
通过控制台,我可以这样做:
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", updated_at: "2011-10-01 19:59:26">]还有:
q.color_ids
=> [1, 3]如何返回返回的颜色名称?例如:["Red", "Blue"]。
发布于 2011-10-02 05:40:16
这将为您提供一个颜色名称数组:
q.colors.map(&:name)https://stackoverflow.com/questions/7623103
复制相似问题