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?
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?
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?
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
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
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?