Rails .each仅显示一次即可获得重复结果

编程入门 行业动态 更新时间:2024-10-24 00:30:08
本文介绍了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仅显示一次即可获得重复结果

本文发布于:2023-11-30 03:26:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648569.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!