需要来自多个的数据:Rails 视图中的多个连接

发布于 2024-08-03 03:42:35 字数 1365 浏览 1 评论 0原文

在大多数情况下,它可能不是最好的解决方案,但我想要一个包含 3 个表数据的表。

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

这就是我想要的视图

<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>

基本上我想要为每个用户对每种媒体的评级添加一个新行

我想要的是一个看起来像这样的表:

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

我认为最好在控制器中使用带有媒体查找方法的联接,但我不这样做确切地知道如何,足够可能的解决方案可以是以媒体和用户作为参数的辅助方法。

您认为最好的解决方案是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琴流音 2024-08-10 03:42:35

这种关联属于您的模型,具有许多直通关系非常适合这种情况。

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

然后您可以访问

media.name media.comment 

然后也可以访问

user.ratings

或:

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

您还可以:

media.ratings.each do |rating|
  rating.your_attribute
  rating.user.your_attribute
end

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文