How would I extract data from this hash?


#1

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.


#2

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)


#3

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/


#4

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!


#5

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


#6

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
removed_email_address@domain.invalid


#7

On Mar 7, 7:50 am, Phlip 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