Forum: Ruby on Rails How would I extract data from this hash?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2c2f3ce7125411ff78a1d3e902aa2a4d?d=identicon&s=25 Bob Sanders (adistarmid)
on 2009-03-07 01:58
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.
Af10464d988bde1b15b1a324237bad84?d=identicon&s=25 Dharmdip Rathod (dharmdip)
on 2009-03-07 05:48
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)
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-03-07 07:04
(Received via mailing list)
> 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/
2c2f3ce7125411ff78a1d3e902aa2a4d?d=identicon&s=25 Bob Sanders (adistarmid)
on 2009-03-07 08:09
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!
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-03-07 08:51
(Received via mailing list)
>> [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??
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-07 13:35
(Received via mailing list)
On Mar 7, 7:50 am, Phlip <phlip2...@gmail.com> 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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2009-03-08 00:46
(Received via mailing list)
On Mar 7, 2009, at 2:50 AM, Phlip wrote:
> Except I forgot the complete conditions, and the inner hash:
> 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@AgileConsultingLLC.com
This topic is locked and can not be replied to.