Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3:How to Sum up Record

I have model Register which marks the attendance of user based on department and group.Each user has a department and group.

Register Model

date:date , departmentid:string , groupid:integer , one:integer , two:integer

among the above fields "one and two" are time slots(say: from 9-14 , 14-18 ).Attendance are marked such that user belongs to current date , groupid , departmentid.So for same day there will be multiple entries with groupid ,departmentid and their attendance.

Sample Register table below

enter image description here

I want to find no. of users attented per day. currently i have tried

@register = Register.where(:date=>"2012-12-28").sum(:one)

it is getting well ,but i want to find like,

@register = Register.where(:date=>"2012-12-28").sum(:one , :two ,:three)

is it possible....

Thanks in advance.

like image 545
Cyber Avatar asked Nov 03 '22 08:11

Cyber


1 Answers

How about this ?

class Register < AR:Base

...

  def self.attendee_count(slot) # slot is :one or :two
    scoped.group(:date ).sum(slot)
  end

end

Register.where(:date => Date.today).attendee_count(:one)
Register.where(:date => Date.today, :departmentid => "DP14").attendee_count(:two)
like image 52
systho Avatar answered Nov 09 '22 15:11

systho