# Records monthwise?

**URL:** <https://www.ruby-forum.com/t/records-monthwise/182673>\
**Category:** Rails\
**Created:** [February 1, 2010, 10:55am UTC](https://www.ruby-forum.com/t/records-monthwise/182673 "2010-02-01T10:55:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [February 1, 2010, 10:55am UTC](https://www.ruby-forum.com/t/records-monthwise/182673/1 "2010-02-01T10:55:51Z")

</div>

Hi Champs, I hope this is not a newbie question.🙂

I have an leave table with columns start\_date and end\_date. Employees  
can apply leave for any date. Now i want to display the number of days  
of leave he had taken in a year… I want it to be displayed like this:-

Jan - 1  
Feb - 10  
Mar - 11  
…  
…  
Dec - 12

Total - Whatever

Can you guys suggest me some way? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [February 1, 2010, 2:10pm UTC](https://www.ruby-forum.com/t/records-monthwise/182673/2 "2010-02-01T14:10:56Z")

</div>

> Hi Champs, I hope this is not a newbie question.🙂  
> Its not as bad as last time, but you may still get yelled at, especially  
> since you’re posting on a monday

Leave.sum(“DAY(end\_date) - DAY(start\_date) + 1”, :group =\>  
“MONTH(start\_date)”, :conditions =\>  
[“YEAR(start\_date)=?”,the\_year\_you\_want])

This query will work, but requires that you handle leaves that begins in  
one month and ends in another by creating one record for each month.  
This could create new problems because that means you can no longer sum  
the number of leaves in a given period by simply counting the number of  
records.

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [February 1, 2010, 3:12pm UTC](https://www.ruby-forum.com/t/records-monthwise/182673/3 "2010-02-01T15:12:46Z")

</div>

Sharagoz – wrote:

I think i am just near to resolve it… Got the logic to it… I’ll tell  
you how i am doing it.

* * *

@month\_leaves = Array.new  
for i in 1…12  
month = i  
year = Date.today.year  
start\_date = Date.parse("#{year}-#{month}-01")  
end\_date = start\_date + 1.month

```
    @month_leaves << Leaves.find(:all, :conditions => [ "employee_id 

```

## = ? and start\_date \>= ? and end\_date \< ?", session[:employee].id, start\_date, end\_date ])

Now two confusions:-

1. Am i assigning the value correct to array… This is correct way …  
Rit … Also tried with @month\_leaves[i] … But cant get it to the views  
page…
2. How can we give the different names in a loop… I mean i do not want  
this @month\_leaves alone handle this situations. But it should be like  
… @month\_1, @month\_2 etc etc …

> > Hi Champs, I hope this is not a newbie question.🙂  
> > Its not as bad as last time, but you may still get yelled at, especially  
> > since you’re posting on a monday
> 
> Leave.sum(“DAY(end\_date) - DAY(start\_date) + 1”, :group =\>  
> “MONTH(start\_date)”, :conditions =\>  
> [“YEAR(start\_date)=?”,the\_year\_you\_want])
> 
> This query will work, but requires that you handle leaves that begins in  
> one month and ends in another by creating one record for each month.  
> This could create new problems because that means you can no longer sum  
> the number of leaves in a given period by simply counting the number of  
> records.
