Rails .each show once for duplicate results

非 Y 不嫁゛ 提交于 2019-12-03 21:53:15

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.

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

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

Try to replace your original code:

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

To:(this version did not broken your code structure, just change the position of the output code)

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

<%= genre_names %> or <%= genre_names.join(" ") %>

Using the uniq method on Array, you'll be able to remove duplicates.

See here for reference: http://www.ruby-doc.org/core-2.1.2/Array.html#method-i-uniq

<% @worlds.collect(&:latin).uniq.each do |latin| %>
  <%= latin.name %>
<% end %>

This assumes there's a belongs_to association between World and Latin (I assumed this because of the World#latin_id field and the fact there's a model called Latin).

EDIT: if you have multiple Latin objects with the same name then collect the names then do uniq:

<% @worlds.collect(&:latin).collect(&:name).uniq.each do |name| %>
  <%= name %>
<% end %>

you can try this to get uniq latin name

 <% @worlds.collect(&:latin).collect(&:name).uniq.each do |latin_name| %>
   <%= latin_name %>
<% end %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!