mark multiple results

15 views
Skip to first unread message

Werner

unread,
Sep 19, 2012, 6:45:10 AM9/19/12
to rubyonra...@googlegroups.com
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.



7stud --

unread,
Sep 19, 2012, 12:09:16 PM9/19/12
to rubyonra...@googlegroups.com
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">]

--
Posted via http://www.ruby-forum.com/.

Javier Quarite

unread,
Sep 19, 2012, 12:27:56 PM9/19/12
to rubyonra...@googlegroups.com
Maybe this helps:


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

Javier

7stud --

unread,
Sep 19, 2012, 2:44:33 PM9/19/12
to rubyonra...@googlegroups.com
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.

Werner

unread,
Sep 19, 2012, 3:02:38 PM9/19/12
to rubyonra...@googlegroups.com
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

Walter Lee Davis

unread,
Sep 19, 2012, 3:57:16 PM9/19/12
to rubyonra...@googlegroups.com

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 so far..
>
> Werner
>
>
>
> On Wednesday, September 19, 2012 8:45:11 PM UTC+2, Ruby-Forum.com User wrote:
> 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.
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/WXndXijvj20J.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Werner

unread,
Sep 20, 2012, 2:16:12 AM9/20/12
to rubyonra...@googlegroups.com
Thanks.. this is helpfull
Greetings
Reply all
Reply to author
Forward
0 new messages