Mark multiple results

Hi…

I have a query
@users = User. …some finding code

<% @users.each do |u| %>

  • <%= u.user_id %><%= u.user.name %>
  • 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

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

    Javier

    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

    On Wednesday, September 19, 2012 8:45:11 PM UTC+2, Ruby-Forum.com User

    On Sep 19, 2012, at 3:02 PM, Werner wrote:

    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

    On Wednesday, September 19, 2012 6:09:48 PM UTC+2, Ruby-Forum.com User