Skip to content

Instantly share code, notes, and snippets.

@rnmp
Created April 13, 2011 15:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rnmp/917816 to your computer and use it in GitHub Desktop.
Save rnmp/917816 to your computer and use it in GitHub Desktop.
Sums the total time (float)
class Project < ActiveRecord::Base
belongs_to :company
has_many :logs
validates_presence_of :name
def total_hours
total_time = 0
logs.each do |l|
total_time += l.time.to_f
end
total_time
end
end
@xenda
Copy link

xenda commented Apr 13, 2011

:)

def total_hours
  total_time = logs.inject(0){|sum,l| sum + l.time.to_f } 
end

@rnmp
Copy link
Author

rnmp commented Apr 13, 2011

Nice, thanks! But I won't change it in order to register the contrast. :)

@xenda
Copy link

xenda commented Apr 13, 2011

Hehe, no problem. Hope it becomes useful next time ;)

@rnmp
Copy link
Author

rnmp commented Apr 13, 2011

It will, really it will.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment