I''m trying to convert a simple: @teams = Team.all into this form: @teams_created = { 1.day.ago => { :teams_created => 100 }, 2.day.ago => { :teams_created => 200 } } Know how I would do it? I''m googling for the answer but I don''t what keywords I should use. -- 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 -~----------~----~----~----~------~----~------~--~---
Bob Sanders wrote:> I''m trying to convert a simple: > > @teams = Team.all > > into this form: > > @teams_created = {1.day.ago => { :teams_created => 100 },2.day.ago => {:teams_created => 200 } > } > > Know how I would do it? I''m googling for the answer but I don''t what > keywords I should use.put your object in between array symbol [@teams] or do << (push operation) -- 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 -~----------~----~----~----~------~----~------~--~---
> I''m trying to convert a simple: > > @teams = Team.all > > into this form: > > @teams_created = {1.day.ago => { :teams_created => 100 },2.day.ago => {:teams_created => 200 } > } > > Know how I would do it? I''m googling for the answer but I don''t what > keywords I should use.[1.day.ago, 2.days.ago].inject({}) do |hash, days| hash[days] = Team.count(:conditions => (days .. days + 1.day)) hash end Ruby can be a brain bender, and a lot of it is google-proof (gag derivative websites notwithstanding). You can''t be expected to google for "inject", or "count_by", if those were indeed the correct "keywords" (actually "methods"). For some of this, you just gotta curl up with an old-fashioned paper book and read it, for a while, for all the details to soak in! -- Phlip http://www.zeroplayer.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 -~----------~----~----~----~------~----~------~--~---
Phlip wrote:>> keywords I should use. > [1.day.ago, 2.days.ago].inject({}) do |hash, days| > hash[days] = Team.count(:conditions => (days .. days + 1.day)) > hash > end > > Ruby can be a brain bender, and a lot of it is google-proof (gag > derivative > websites notwithstanding). You can''t be expected to google for "inject", > or > "count_by", if those were indeed the correct "keywords" (actually > "methods"). > > For some of this, you just gotta curl up with an old-fashioned paper > book and > read it, for a while, for all the details to soak in! > > -- > Phlip > http://www.zeroplayer.com/Thanks Phlip! Really appreciate your generous help. I just discovered the "inject" method, and you pointed me in the perfect direction. Thank you! -- 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 -~----------~----~----~----~------~----~------~--~---
>> [1.day.ago, 2.days.ago].inject({}) do |hash, days| >> hash[days] = Team.count(:conditions => (days .. days + 1.day)) >> hash >> end> Thanks Phlip! Really appreciate your generous help. I just discovered > the "inject" method, and you pointed me in the perfect direction. Thank > you!Except I forgot the complete conditions, and the inner hash: hash[days] = { :teams_created => Team.count(:conditions => { :created_on => (days .. days + 1.day) }) } Does anyone know if this would work? hash[days] Team.count(:select => ''COUNT(teams.*) AS teams_created'', :conditions => { :created_on => (days .. days + 1.day) }).attributes And is there a way to use :group to get it down to just one (1) query?? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Mar 7, 7:50 am, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> And is there a way to use :group to get it down to just one (1) query??Team.count :all, :group => ''DATE(created_at)'' Fred --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 7, 2009, at 2:50 AM, Phlip wrote:>>> [1.day.ago, 2.days.ago].inject({}) do |hash, days| >>> hash[days] = Team.count(:conditions => (days .. days + 1.day)) >>> hash >>> end > >> Thanks Phlip! Really appreciate your generous help. I just discovered >> the "inject" method, and you pointed me in the perfect direction. >> Thank >> you! > > Except I forgot the complete conditions, and the inner hash: > > hash[days] = { :teams_created => > Team.count(:conditions => { :created_on => (days .. days + > 1.day) }) } > > Does anyone know if this would work? > > hash[days] > Team.count(:select => ''COUNT(teams.*) AS teams_created'', > :conditions => { :created_on => (days .. days + > 1.day) }).attributes > > And is there a way to use :group to get it down to just one (1) > query??I''m jumping into this thread late so ignore me if I''m repeating something already said or am missing some context. hash = {} Team.find(:all, :select => ''DATE(created_on) AS created_on, COUNT(*) AS teams_created'', :conditions => [''created_on > ?'', 2.days.ago], :group => ''DATE(created_on)'').each do |t| hash[t[''created_on'']] = t[''teams_created''].to_i end Should result in hash containing something like: { ''2009-03-05'' => 19, ''2009-03-06'' => 14, ''2009-03-07'' => 5 } Is this something that helps? -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---