Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed data from a many:many join in a Rails view
    text
    copied!<p>Its maybe not the best solution in most cases, but i want a table with data form 3 tables.</p> <pre><code>class Media &lt; ActiveRecord::Base belongs_to :user belongs_to :type has_many :ratings end class User &lt; ActiveRecord::Base has_many :medias has_many :ratings end class Rating &lt; ActiveRecord::Base belongs_to :user belongs_to :media end </code></pre> <p>Thats the view I want</p> <pre><code>&lt;table&gt; &lt;tr&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Comment&lt;/th&gt; &lt;th&gt;Creator&lt;/th&gt; &lt;th&gt;Type&lt;/th&gt; &lt;% for user in @users %&gt; &lt;th&gt;&lt;%=h user.login %&gt;&lt;/th&gt; &lt;% end %&gt; &lt;/tr&gt; &lt;% for media in @medias %&gt; &lt;tr&gt; &lt;td&gt;&lt;%=h media.name %&gt;&lt;/td&gt; &lt;td&gt;&lt;%=h media.comment %&gt;&lt;/td&gt; &lt;td&gt;&lt;%=h media.user.login %&gt;&lt;/td&gt; &lt;td&gt;&lt;%=h media.type.name %&gt;&lt;/td&gt; &lt;% for user in @users %&gt; &lt;td&gt;&lt;%=h GET_RATING (media, user) %&gt;&lt;/td&gt; &lt;% end %&gt;%&gt; &lt;/tr&gt; &lt;% end %&gt; &lt;/table&gt; </code></pre> <p>Basicly i want one new row for each users ratings for each media</p> <p>What I want is a Table that looks like that:</p> <pre><code>media.name media.comment ... rating(media, user).rating </code></pre> <p>I think it would be better to use a join in the Controller with the Media find methods but I dont know how exactly, enougher possible solution could be helper method that takes media and user as parameters.</p> <p>What do you think is the best solution for this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload