Question

I have a @minisets model and a @miniatures model. They both have_many of each other through the @contents model.

As well as the foreign keys, the @contents model also has a quantity column.

From my @minisets show view I can show the associated @miniatures with the following:

<% @miniset.miniatures.each do |miniature| %>
      <%= link_to miniature.name, miniature %>
<% end %>

I want to be able to show the quantity entered for those miniatures but can't work out how to call information from the join table rather than the table it is joining.

Something like <%= miniature.content.quantity %> except that won't work. I assume the joining model must be in play for it to be supplying the joined info but how do I interact with it itself in that instance?

Was it helpful?

Solution

Figured it out.

I need to be working with the join object in the instance variable rather than the joined object.

Find the @contents which belong to this @miniset and then get the @miniature info from there. Makes much more sense.

<% @miniset.contents.where(miniset_id: @miniset).each do |content| %>
          <%= link_to content.miniature.name, content.miniature %>
          x<%= content.quantity %>        
<% end %>

Found some very complicated answers to similar questions but this is dead simple. Hope it helps someone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top