views:

73

answers:

1

Its maybe not the best solution in most cases, but i want a table with data form 3 tables.

class Media < ActiveRecord::Base
  belongs_to :user
  belongs_to :type
  has_many :ratings                                           
end

class User < ActiveRecord::Base
  has_many :medias
  has_many :ratings
end

class Rating < ActiveRecord::Base
  belongs_to :user
  belongs_to :media
end

Thats the view I want

<table>
  <tr>
    <th>Name</th>
    <th>Comment</th>
    <th>Creator</th>
    <th>Type</th>
    <% for user in @users %>
      <th><%=h user.login %></th>
    <% end %>
  </tr>

<% for media in @medias %>
  <tr>
    <td><%=h media.name %></td>
    <td><%=h media.comment %></td>
    <td><%=h media.user.login %></td>
    <td><%=h media.type.name %></td>
    <% for user in @users %>
      <td><%=h GET_RATING (media, user) %></td>
    <% end %>%>
  </tr>
<% end %>
</table>

Basicly i want one new row for each users ratings for each media

What I want is a Table that looks like that:

media.name  media.comment ...   rating(media, user).rating

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.

What do you think is the best solution for this?

+1  A: 

This kind of association belongs in your model, a has many through relationship is perfect for this.

class User < ActiveRecord::Base
  has_many :ratings
  has_many :media, :through => :ratings
end

class Media < ActiveRecord::Base
  has_many :ratings
  has_many :users, :through => ratings
end

class Rating < ActiveRecord::Base
  belongs_to :user
  belongs_to :media
end

Then you can access

media.name media.comment

Then could also access

user.ratings

or:

<% media.users.each do |user| %>
  ## Do stuff with user.ratings array
<% end %>

You can also:

media.ratings.each do |rating|
  rating.your_attribute
  rating.user.your_attribute
end
railsninja
thanx that helped a lot, but I get it not running complete: <% media.users.each do |rater| %> <% rater.ratings.each do |rating| %> <td><%=h rating.rating %></td> <% end %> <% end %>that is what i have at the moment, the problem is when a user have not given a rating he is not in the list media.users so i get a table with different td count per line.
no it does not work.with media.users I get all users who have have give a rating for that movie. with media.users.ratings I get all ratings wich the user gave but not just for that media. So if i give a rating As user A for movie 1 and another rating for movie 2 and then I get for movie 1 both votings and also for the movie 2 both votings.So the solution is invalid or only partial.
This the right technique. You need to populate the association.
railsninja
K I marked it as answerd because I think you did. At the moment I use another code for the problem "custom fields" where the user can set an heading and a value so that code isnt in the app anymore. But I learned much from it. Thanx