ruby on rails group by with null values problem

Posted by winter sun on Stack Overflow See other posts from Stack Overflow or by winter sun
Published on 2011-02-28T23:11:58Z Indexed on 2011/02/28 23:24 UTC
Read the original article Hit count: 314

Filed under:
|

I have an hour table in witch I store user time tracking information, the table consists from the following cells

project_id
task_id (optional can be null)
worker_id
reported_date
working_hours

each worker enters sevral records per day so generally the table is looking like this

id project_id  worker_id task_id   reported_date  working hours;    
== =========== ========= ========= =============  ==============
1  1           1         1         10/10/2011      4                 
2  1           1         1         10/10/2011     14                 
3  1           1                   10/10/2011      4                 
4  1           1                   10/10/2011     14                 

the task_id is not a must field so there can be times when the user is not selecting it and ther task_id cell is empty
now i need to display the data by using group by clouse so the resualt will be somthing like this

project_id worker_id task_id   working hours
========== ========= ========= ==============
1          1         1         18                 
1          1                   18            

I did the folowing group by condition

@group_hours=Hour.group('project_id,worker_id,task_id)').select('project_id, task_id ,worker_id,sum(working_hours)as working_hours_sum')

My view looks like this

<% @group_hours.each do |b| %>

<%= b.project.name if b.project %>
<%= b.worker.First_name if b.worker %>
<%= b.task.name if b.task %>
<%= b.working_hours_sum %>


<%end%>

This it is working but only if the task_id is not null when task id is null it present all the records without grouping them like this

project_id  worker_id task_id   working hours
=========== ========= ========= ==============
1           1         1         18                 
1           1                    4            
1           1                   14    

I will appreciate any kind of solution to this problem

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby