John Abraham
John Abraham

Reputation: 18781

How to displaying an specific attributes with a many to many association in rails

I have a two way many-to-many assoication between 3 models: work.rb, category.rb, categorywork.rb

Within the work#index using <%= work.categories %> renders some wonky looking html markup

wonky

<% @works.each do |work| %>
  <tr>
    <td><%= work.name %></td>
    <td><%= work.subtitle %></td>
    <td><%= work.categories %></td>

    <td><%= link_to 'Show', work %></td>
    <td><%= link_to 'Edit', edit_work_path(work) %></td>
    <td><%= link_to 'Destroy', work, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>

I'm trying to target speific attributes of the association like "name".

Unfortunately when using <%= work.categories.name %> it gets weirder with: wonkier

How do i target just the name or just the description?

Upvotes: 0

Views: 20

Answers (1)

t56k
t56k

Reputation: 6981

Try this out:

<%= work.categories.pluck(:name) %>

Upvotes: 1

Related Questions