0

I've got this bit of code which returns an array of IDs like [1, 2, 3]:

<% @articles.each do |a| %>
  <%= a.brand_ids %>
<% end %>

Article and Brand have a has_many, :through relationship. How would I return a list of the names of each brand instead of IDs? a.brands.name returns Brand. Any thoughts?

t56k
  • 6,769
  • 9
  • 52
  • 115

1 Answers1

1

Use pluck:

article = Article.find(17)
article.brands.pluck(:name)
dwhalen
  • 1,927
  • 12
  • 11
  • brand model has `translates :name` attribute, hence I can't do `brands.pluck(:name)`, although `brands.first.name` works. How do I pluck the names of all the brands from the translation table? – Vipin Verma May 24 '16 at 10:28