Is it generally preferred to filter a large collection of objects in memory or in the database. In the case of a single query, I know that the database is preferred. But what about multiple filtering based on atributes? For example, @users = User.find(:all) @males = @users.find_all {|user| user if user.gender == ''male''} @females = @users.find_all {|user| user if user.gender =''female''} Then these are filtered AGAIN in the view: <tr> <td>males</td> <td><%= @males.find_all{|user| user.ethnicity == ''white''}.size %></td> <td><%= @males.find_all{|user| user.ethnicity == ''black''}.size %></td> <td><%= @males.find_all{|user| user.ethnicity == ''asian''}.size %></td> <td><%= @males.find_all{|user| user.ethnicity == ''hispanic''}.size %></td> <td><%= @males.find_all{|user| user.ethnicity == ''other''}.size %></td> </tr> My question is - should i hit the database separately for each of these groups and subgroups, or filter in memory as I am doing now? Eventually there could be 100,000+ records. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Taylor Strait wrote:> Is it generally preferred to filter a large collection of objects in > memory or in the database. In the case of a single query, I know that > the database is preferred. But what about multiple filtering based on > atributes? For example, > > @users = User.find(:all) > @males = @users.find_all {|user| user if user.gender == ''male''} > @females = @users.find_all {|user| user if user.gender => ''female''} > > Then these are filtered AGAIN in the view: > > <tr> > <td>males</td> > <td><%= @males.find_all{|user| user.ethnicity == ''white''}.size %></td> > <td><%= @males.find_all{|user| user.ethnicity == ''black''}.size %></td> > <td><%= @males.find_all{|user| user.ethnicity == ''asian''}.size %></td> > <td><%= @males.find_all{|user| user.ethnicity == ''hispanic''}.size > %></td> > <td><%= @males.find_all{|user| user.ethnicity == ''other''}.size %></td> > </tr> > > My question is - should i hit the database separately for each of these > groups and subgroups, or filter in memory as I am doing now? Eventually > there could be 100,000+ records.First a general and unhelpful answer: try both (maybe fake up some data to see what happens as the database groups) and see what happens. In this case you''re only (at least what you''ve shown) using the number of people of the various types, so you could do something like User.male.count :group => ''ethnicity'' (assuming you had a male named scope doing the obvious) which will get you the counts by ethnicity Fred> -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> In this case you''re only (at least what you''ve shown) using the number > of people of the various types, so you could do something like > > User.male.count :group => ''ethnicity'' > (assuming you had a male named scope doing the obvious) which will get > you the counts by ethnicity > > FredThanks. I will give that a try. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---