Created
January 28, 2016 11:25
-
-
Save beliar91/5242a2e7e132e414a6b1 to your computer and use it in GitHub Desktop.
models/Task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Task < ActiveRecord::Base | |
validates :status, inclusion: {in: ["Created", "In Progress", "Completed"]} | |
scope :by_status, -> (status) {where(status: status)} | |
after_create :count_average_execution_time | |
belongs_to :house_hold | |
def count_average_execution_time | |
tasks = Task.by_status(self.status) | |
sum = 0 | |
tasks.each do |task| | |
sum+= task.completion_time | |
end | |
number_of_tasks = Task.by_status(self.status).count | |
average = sum / number_of_tasks | |
p "Average execution time for task with status: #{self.status} is " << average.to_s | |
self.avg_completion_time = average | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment