Rails .each仅显示一次即可获得重复结果 [英] Rails .each show once for duplicate results

查看:93
本文介绍了Rails .each仅显示一次即可获得重复结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在rails .each 代码中,如何从结果中消除重复项?

Within a rails .each code, how can I eliminate duplicates from the result?

PMRelationships是一个连结人物和电影的关系表
MGRelationships是连结类型和电影的关系表

PMRelationships is a relationship table that links people and movies MGRelationships is a relationship table that links genres and movies

我要寻找的过程是首先找到一个人的所有类型PmRelationship然后是MgRelationship

The process Im looking for is to find All a Person's genres through first PmRelationship then MgRelationship

<% @pm_relationships = PmRelationship.where(:people_id => @person.id) %>

<ul class="basic-info-genres">
  <% @pm_relationships.each do |pm_relationship| %>
    <% @movie=Movie.find(pm_relationship.movie_id) %>
    <% @mg_relationships = MgRelationship.where(:movie_id => @movie.id) %>
    <% @mg_relationships.each do |mg_relationship| %>
      <% @genre=Genre.find(mg_relationship.genre_id) %>
      <%= @genre.name %>
    <% end %>
  <% end %>
</ul>

例如,此代码返回值列表,例如:

For example, this code returns a list of values, as such:

动作
动作
喜剧
惊悚片
冒险
黑色黑色
动作

Action Action Comedy Thriller Adventure Noir Action

如何做到这一点,以便它删除两个Lorems,仅显示一个

How can I make it so that It removes the two Lorems and only show one

基本上,消除所有重复以返回这样的列表

Basically, Get rid of any duplication to return a list as such

动作
喜剧
惊悚片
冒险
黑色

Action Comedy Thriller Adventure Noir

UPDATE

UPDATE

我已更改了简化代码以使其与实际应用程序的代码匹配

I've changed my simplified code to match that of my actual app

我知道其中大部分需要在控制器中,但为简化起见,我首先在视图中进行操作

I am aware most of this needs to be in the controller but simplified sake I'm doing it in the views first

那些可能会提到我不需要创建_Relationship表并且可以仅使用模型has_many_belongs_to等的人,我发现Relationship table方法更易于处理和控制

Side note to those who could mention that I needn't create _Relationship tables and could simply use models has_many_belongs_to etc.., I find the Relationship table method much easier to handle and control

谢谢并为此感到歉意

推荐答案

如您所知,您可以使用关联,因此我强烈建议您使用它。

As you are aware that your can use associations, I highly recommend it. It will help you to clean up the code and will save database queries.

另一件事,您缺少< li>< / li> ; < ul>< / ul> 内。

Another thing, you are missing <li></li> inside <ul></ul>.

根据您当前的代码,您只需在 Set

Anyways, as per your current code, you can just populate your all @genre.name in a Set

# as you said in question, following code should be moved to controller
genre_names = Set.new
<% @pm_relationships = PmRelationship.where(:people_id => @person.id) %>

<% @pm_relationships.each do |pm_relationship| %>
  <% @movie=Movie.find(pm_relationship.movie_id) %>
  <% @mg_relationships = MgRelationship.where(:movie_id => @movie.id) %>

  <% @mg_relationships.each do |mg_relationship| %>
    <% @genre=Genre.find(mg_relationship.genre_id) %>
    <% genre_names.add(@genre.name) %>    
  <% end %>
<% end%>

# actual view code
<ul class="basic-info-genres">
  <%= "<li>#{genre_names.to_a.join('</li><li>')}</li>".html_safe %>
</ul>

也许您想阅读有关 设置

May be you want to read more about Set

注意:将代码移至相应文件后,根据需要在控制器和视图中使用适当的变量类型

NOTE: After moving code to corresponding files, use appropriate variable types in controller and view, as required

这篇关于Rails .each仅显示一次即可获得重复结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆