How would I extract data from this hash?

I'm trying to convert a simple:

@teams = Team.all

into this form:

   @teams_created = { => { :teams_created => 100 }, => {:teams_created => 200 }

Know how I would do it? I'm googling for the answer but I don't what
keywords I should use.

   [, 2.days.ago].inject({}) do |hash, days|
     hash[days] = Team.count(:conditions => (days .. days +

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!

[, 2.days.ago].inject({}) do |hash, days|
     hash[days] = Team.count(:conditions => (days .. days +

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 + }) }

Does anyone know if this would work?

      hash[days] =
             Team.count(:select => 'COUNT(teams.*) AS teams_created',
          :conditions => { :created_on => (days .. days + }).attributes

And is there a way to use :group to get it down to just one (1) query??

Team.count :all, :group => 'DATE(created_at)'


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

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 Biedenharn