简体   繁体   English

我如何去除红宝石中的骗子

[英]How do i remove the dupes in ruby

I have this loop in rails 我在轨道上有这个循环

 - @companies.people.each do |person|
  %p
    Hello there :
    = "#{person.manager.name} (#{person.manager.email})"

but i only want to print the managers name once.....but lots of people have the same manager and they are printing dupes...any idea how to not print dupes here 但是我只想打印一次经理的名字.....但是很多人都有同一位经理,而且他们正在打印伪造品...任何想法如何在这里不打印伪造品

Wouldn't you rather do: 您是否愿意:

@companies.managers do |manager|
...

So you need to amend the underlying model (Company?) with a managers method. 因此,您需要使用managers方法修改基础模型(公司?)。 And whether that's done via a scope, or a model relation or alfonso's brute force answer, we don't have enough information to determine. 而且,无论是通过范围,模型关系还是alfonso的蛮力回答来完成,我们都没有足够的信息来确定。 But in any case this logic is best tucked away in the model and not exposed in the view. 但是无论如何,最好将这种逻辑隐藏在模型中,并且不要在视图中公开。

class Company
  scope :managers, ->(){where(manager: true)}
end

module EmployeeListViewHelper
  def manager_list
    Company.managers.each do |m|
      content_tag(:p, "Hello There : #{m.name} #{m.email}")
    end
  end
end

Then just this in your view: 然后在您看来就是这样:

= manager_list

Well, it looks like you're going about this probably the wrong way. 好吧,看来您正在采用这种错误的方式。 If you don't want the manager's name duplicated for each person, you might have to group people under managers. 如果您不希望每个人都重复经理的姓名,则可能必须将人归为经理。

Your view then should look hierarchical, people under the manager should be visually placed like that, as well. 然后,您的视图应看起来是分层的,管理者下的人员也应在视觉上这样放置。

您可以使用uniq方法执行此操作:

@companies.people.map{|p| p.manager}.uniq

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM