# Adding values from a db the rails way?

**URL:** <https://www.ruby-forum.com/t/adding-values-from-a-db-the-rails-way/59177>\
**Category:** Rails\
**Created:** [April 30, 2006, 2:57pm UTC](https://www.ruby-forum.com/t/adding-values-from-a-db-the-rails-way/59177 "2006-04-30T14:57:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [April 30, 2006, 2:57pm UTC](https://www.ruby-forum.com/t/adding-values-from-a-db-the-rails-way/59177/1 "2006-04-30T14:57:34Z")

</div>

I have a log table which has a column recording the number of hours  
worked on a task. So there’ll be multiple lines for a particular task.

I want to retrieve the total number of hours worked on a task, so that I  
can include the information when one views the task details.

I can write a sql query to do this easily enough, like “SELECT  
SUM(hours) AS total FROM tasklog WHERE task\_id = x”, but what’s the  
rails way to do this?

thx  
n.

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [May 1, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/adding-values-from-a-db-the-rails-way/59177/2 "2006-05-01T14:17:07Z")

</div>

I’ve solved it like this:

def self.time\_logged\_for\_task(this\_task)  
self.connection.select\_value(“SELECT SUM(hours) FROM timelogs tl  
JOIN tasks t ON tl.task\_id = t.id WHERE tl.task\_id=” + this\_task.to\_s)  
end

If there’s a better way, please let me know! 😉

n.

Nick C. wrote:

> I have a log table which has a column recording the number of hours  
> worked on a task. So there’ll be multiple lines for a particular task.
> 
> I want to retrieve the total number of hours worked on a task, so that I  
> can include the information when one views the task details.
> 
> I can write a sql query to do this easily enough, like “SELECT  
> SUM(hours) AS total FROM tasklog WHERE task\_id = x”, but what’s the  
> rails way to do this?
> 
> thx  
> n.

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [May 1, 2006, 2:44pm UTC](https://www.ruby-forum.com/t/adding-values-from-a-db-the-rails-way/59177/3 "2006-05-01T14:44:31Z")

</div>

check out ActiveRecord::Calculations  
([Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Calculations/ClassMethods.html))

you can do:

Timelog.sum(‘hours’, :conditions =\> [‘task\_id=?’, this\_task])

based on what i am seeing you don’t need the join

Nick C. wrote:

> I’ve solved it like this:
> 
> def self.time\_logged\_for\_task(this\_task)  
> self.connection.select\_value(“SELECT SUM(hours) FROM timelogs tl  
> JOIN tasks t ON tl.task\_id = t.id WHERE tl.task\_id=” + this\_task.to\_s)  
> end
> 
> If there’s a better way, please let me know! 😉
> 
> n.
> 
> Nick C. wrote:
> 
> > I have a log table which has a column recording the number of hours  
> > worked on a task. So there’ll be multiple lines for a particular task.
> > 
> > I want to retrieve the total number of hours worked on a task, so that I  
> > can include the information when one views the task details.
> > 
> > I can write a sql query to do this easily enough, like “SELECT  
> > SUM(hours) AS total FROM tasklog WHERE task\_id = x”, but what’s the  
> > rails way to do this?
> > 
> > thx  
> > n.

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [May 1, 2006, 3:34pm UTC](https://www.ruby-forum.com/t/adding-values-from-a-db-the-rails-way/59177/4 "2006-05-01T15:34:18Z")

</div>

That’s what I was looking for! I needed the join for some more  
conditions, so it now looks like:

def self.time\_logged\_for\_task(this\_task)  
Timelog.sum(‘hours’,  
:conditions =\> [‘task\_id=? OR t.parent\_id=?’,  
this\_task.to\_s, this\_task.to\_s],  
:joins =\> “JOIN tasks t ON task\_id = t.id”)  
end

thx!  
n.

jeremy wrote:

> check out ActiveRecord::Calculations  
> ([Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Calculations/ClassMethods.html))
> 
> you can do:
> 
> Timelog.sum(‘hours’, :conditions =\> [‘task\_id=?’, this\_task])
> 
> based on what i am seeing you don’t need the join
> 
> Nick C. wrote:
> 
> > I’ve solved it like this:
> > 
> > def self.time\_logged\_for\_task(this\_task)  
> > self.connection.select\_value(“SELECT SUM(hours) FROM timelogs tl  
> > JOIN tasks t ON tl.task\_id = t.id WHERE tl.task\_id=” + this\_task.to\_s)  
> > end
> > 
> > If there’s a better way, please let me know! 😉
> > 
> > n.

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [May 3, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/adding-values-from-a-db-the-rails-way/59177/5 "2006-05-03T18:56:25Z")

</div>

Hello Nick,

> I have a log table which has a column recording the number of hours  
> worked on a task. So there’ll be multiple lines for a particular task.
> 
> I want to retrieve the total number of hours worked on a task, so that I  
> can include the information when one views the task details.
> 
> I can write a sql query to do this easily enough, like “SELECT  
> SUM(hours) AS total FROM tasklog WHERE task\_id = x”, but what’s the  
> rails way to do this?

See ActiveRecord::Calculations in Rails 1.1

@total = Tasklog.sum(‘hours’, :conditions =\> [‘task\_id = ?’, x] )

But maybe when manipulating time durations, it’s easier to store  
minutes instead of hours ?  
0.33333 hour = 20 minutes.

Then when you display durations, you convert in hours and minutes.  
Just an idea… Well, it’s up to you.

```
 -- Jean-FranÃ§ois.
```
