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
[email protected]
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---