I'm building a call tracking application, as a way to learn Rails and Twilio. Right now, I am trying to follow Ryan Bates' tutorial to create a graph that shows users the number of phone calls they have per day.
The model works like so -- > Phone has_many calls
What I would like to do, is to create a class method that would allow me to do something like
phone.calls.total_on(date)
This would show the total calls a particular phone has on a particular date.
Here's the code currently in my Phone model
def self.total_on(date)
where("date(calls_at) = ?", date).sum( self.calls.count )
end
What probably is wrong with the code is sum(self.calls.count). This would count all the calls, regardless of date, if I'm in the right direction.
How would I go about making a class method that would count the calls on a particular date?
Thanks for sticking with me so far! Your thoughts greatly appreciated.