mark multiple results

Hi…

I have a query

@users = User. …some finding code

<% @users.each do |u| %>
<li><%= u.user_id %><%= u.user.name %></li>


Result is something like this:
•   1 fred
•   1 fred
•   9 charlie

The result could be 10 different user and/or some could be double or triple. I want to css mark them when they appear multipe. How is the best way to find out which user_id’s are size/count >=2 in
user?
To make it clear: I dont want to find out double entries in my model, but in the result.

Thanks for support.




class User
  attr_accessor :id, :name

  def initialize(id, name)
    @id = id
    @name = name
  end
end

charlie = User.new(9, 'Charlie')
fred = User.new(1, 'Fred')
sally = User.new(3, 'Sally')

@users = [
  fred,
  fred,
  fred,
  sally,
  charlie,
  sally
]

user_counts = Hash.new(0)

@users.each {|user| user_counts[user] += 1}
p user_counts
puts

--output:--
{#<User:0x00000100867be8 @id=1, @name="Fred">=>3,
#<User:0x00000100867b70 @id=3, @name="Sally">=>2,
#<User:0x00000100867d00 @id=9, @name="Charlie">=>1}

ordered_results = user_counts.sort_by{|user| -user_counts[user]}
p ordered_results
puts

--output:--
[[#<User:0x00000100867be8 @id=1, @name="Fred">, 3],
[#<User:0x00000100867b70 @id=3, @name="Sally">, 2],
[#<User:0x00000100867d00 @id=9, @name="Charlie">, 1]]

cutoff = 2
most_appearances = ordered_results.take_while {|user, count| count >=
cutoff}
p most_appearances
puts

--output:--
[[#<User:0x00000100867be8 @id=1, @name="Fred">, 3],
[#<User:0x00000100867b70 @id=3, @name="Sally">, 2]]

top_users = most_appearances.map {|user, count| user}
p top_users

--output:--
[#<User:0x00000100867be8 @id=1, @name="Fred">, #<User:0x00000100867b70
@id=3, @name="Sally">]

Maybe this helps:

http://api.rubyonrails.org/classes/Enumerable.html#method-i-group_by

http://railscasts.com/episodes/29-group-by-month

You may do @users.group_by(&:id) … but I’m not sure what you have in your search methods

Javier

Javier Quarite wrote in post #1076666:

Maybe this helps:

http://api.rubyonrails.org/classes/Enumerable.html#method-i-group_by
http://railscasts.com/episodes/29-group-by-month

You may do @users.group_by(&:id) ... but I'm not sure what you have in

Each user has a unique id, so what does that get you? Groups with one
user per group.

Hi Javier…

I want to show all entries…they have different other attributes but the same user (name)…thats why group_by is not what I can use.
Just want to mark them ‘bold’ …so I need a true/false for every result item…
Thanks so far…

Werner

Hi Javier..

I want to show all entries..they have different other attributes but the same user (name)..thats why group_by is not what I can use.
Just want to mark them 'bold' ..so I need a true/false for every result item..

You could do this with JavaScript pretty easily. Assuming you have a UL full of results, your code could look like this (Prototype.js):

  var users = $$('#my_list li');
  users.each(function(elm){
    if(elm.siblings().include(function(s){ return s.innerHTML == elm.innerHTML; }).length > 0){
      elm.addClassName('multiple');
    }
  });

Walter

Thanks… this is helpfull
Greetings