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. 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. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.