Jim..sorry.. not my day...
have to contemplate about your solution.

I wonder..

HourUser.includes(:user).where.....group_by { |h| h.week_id }

<% @hours.keys.sort.each do |h| %>
 <%= @hours[h].collect(&:hour).sum %></td>
<% end %>

is giving me what I want, just needs to be grouped by user_id

Thanks so far.



Am Donnerstag, 24. Januar 2013 14:16:16 UTC+1 schrieb jim:
>
>
>
>
> On Thu, Jan 24, 2013 at 7:55 PM, Werner 
> <[email protected]<javascript:>
> > wrote:
>
>> Hi Jim.. thanks so far..
>>
>> in the moment this is a bit too far for me.
>>
>>
>> Just remember that to get a certain value, you'll have to pass an array 
>> as the index
>> ie sums[[33,2]] to get 70
>> => this is unclear
>> Pls. be so kind to explain the view part.
>>
>
> since the keys of the hash is an array, you need to use an array as the 
> index to get a value
>
> >> sums = { [33, 2] => 70, [34, 2] => 15, [35, 3] => 20 }
> >> sums[34,2]
> ArgumentError: wrong number of arguments (2 for 1)
> from (irb):3:in `[]'
> from (irb):3
> >> sums[[34,2]] # 15
>
> the keys are defined by the group option you passed to #sum, so if you 
> pass as sql statement to
> the group option, you'll get that as key. ie (postgre)
>
> >> HourUser.sum(:hours, group: "week_id || ' --- ' || user_id", order: 
> :user_id)
> >> { '33 --- 2' => 70, '34 --- 2' => 15, '35 --- 3' => 20 }
>
> hope this helps
>
>  
>
>>
>> Werner
>>
>>
>>
>>
>>
>>
>> Am Donnerstag, 24. Januar 2013 11:36:44 UTC+1 schrieb jim:
>>>
>>>
>>>
>>> On Thu, Jan 24, 2013 at 4:35 PM, Werner <webagent...@googlemail.**com>wrote:
>>>
>>>> Hi.. I need some support...
>>>>
>>>> table:
>>>> week_id, user_id, project_id, hours
>>>> ex. => 
>>>> 33, 2, 1, 10
>>>> 34, 2,1,15
>>>> 33, 2, 2, 20
>>>> 35, 3, 1,20
>>>> etc.
>>>
>>>
>>>> Want to display a sum of hours per week_id per user_id
>>>> I have:
>>>>
>>>> @hours = HourUser.includes(:user).**group_by { |h| h.week_id }
>>>>
>>>> @hours.keys.sort.each do |hour|
>>>> @hours[hour].collect(&:**stunden).sum
>>>>
>>>
>>> Look at http://api.rubyonrails.org/**classes/ActiveRecord/**
>>> Calculations.html#method-i-sum<http://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-sum>
>>>
>>> sums = HourUser.sum(:hours, group: [:week_id, :user_id], order: :user_id)
>>>
>>> You'll end up with something like [33, 2] => 70, [34, 2] => 15, [35, 3] 
>>> => 20
>>> Just remember that to get a certain value, you'll have to pass an array 
>>> as the index
>>> ie sums[[33,2]] to get 70
>>>
>>>  
>>>
>>>>  
>>>> Hours are summed up, but not sorted by user_id..
>>>> How to get that?
>>>>
>>>> Thanks 
>>>> Werner
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>  -- 
>>>> 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 rubyonra...@googlegroups.**com.
>>>> To unsubscribe from this group, send email to rubyonrails-ta...@**
>>>> googlegroups.com.
>>>>
>>>> To view this discussion on the web visit https://groups.google.com/d/**
>>>> msg/rubyonrails-talk/-/**nIwEcQd5RUMJ<https://groups.google.com/d/msg/rubyonrails-talk/-/nIwEcQd5RUMJ>
>>>> .
>>>> For more options, visit 
>>>> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
>>>> .
>>>>  
>>>>  
>>>>
>>>
>>>
>>>
>>> -- 
>>> ------------------------------**------------------------------**-
>>> visit my blog at http://jimlabs.heroku.com 
>>>
>>  -- 
>> 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]<javascript:>
>> .
>> To unsubscribe from this group, send email to 
>> [email protected] <javascript:>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msg/rubyonrails-talk/-/CDisLJSEMNEJ.
>>
>> For more options, visit https://groups.google.com/groups/opt_out.
>>  
>>  
>>
>
>
>
> -- 
> -------------------------------------------------------------
> visit my blog at http://jimlabs.heroku.com 
>

-- 
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].
To view this discussion on the web visit 
https://groups.google.com/d/msg/rubyonrails-talk/-/rs-cau_8-hcJ.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to