문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top