Records monthwise?

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

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.

Hi Champs, I hope this is not a newbie question.:slight_smile:
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.

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.:slight_smile:
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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs