# How would I extract data from this hash?

**URL:** <https://www.ruby-forum.com/t/how-would-i-extract-data-from-this-hash/162228>\
**Category:** Rails\
**Created:** [March 7, 2009, 1:58am UTC](https://www.ruby-forum.com/t/how-would-i-extract-data-from-this-hash/162228 "2009-03-07T01:58:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [March 7, 2009, 1:58am UTC](https://www.ruby-forum.com/t/how-would-i-extract-data-from-this-hash/162228/1 "2009-03-07T01:58:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [March 7, 2009, 5:48am UTC](https://www.ruby-forum.com/t/how-would-i-extract-data-from-this-hash/162228/2 "2009-03-07T05:48:43Z")

</div>

Bob S. 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)

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [March 7, 2009, 7:04am UTC](https://www.ruby-forum.com/t/how-would-i-extract-data-from-this-hash/162228/3 "2009-03-07T07:04:44Z")

</div>

> 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/](http://www.zeroplayer.com/)

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [March 7, 2009, 8:09am UTC](https://www.ruby-forum.com/t/how-would-i-extract-data-from-this-hash/162228/4 "2009-03-07T08:09:28Z")

</div>

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/](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!

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [March 7, 2009, 8:51am UTC](https://www.ruby-forum.com/t/how-would-i-extract-data-from-this-hash/162228/5 "2009-03-07T08:51:17Z")

</div>

> > [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??

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [March 8, 2009, 12:46am UTC](https://www.ruby-forum.com/t/how-would-i-extract-data-from-this-hash/162228/6 "2009-03-08T00:46:37Z")

</div>

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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [March 7, 2009, 1:35pm UTC](https://www.ruby-forum.com/t/how-would-i-extract-data-from-this-hash/162228/7 "2009-03-07T13:35:35Z")

</div>

On Mar 7, 7:50 am, Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
