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. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/9K8I4HJD0pIJ. For more options, visit https://groups.google.com/groups/opt_out.
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/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
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 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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. > >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Thanks.. this is helpfull Greetings On Wednesday, September 19, 2012 6:09:48 PM UTC+2, Ruby-Forum.com User wrote:> > 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/. >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/1u9hqi__OjMJ. For more options, visit https://groups.google.com/groups/opt_out.