Why isn't this use of inject working?
Ruby:
p "STARTING"
notes = notes.find_all{|note| note.date_occurred == date}
p notes.class
p notes.inject{|sum, note| sum + note.time_spent }
Output:
"STARTING"
Array
#<Note id: 82, time_spent: 开发者_如何学C5, created_at: "2011-08-29 00:32:26", updated_at: "2011-08-29 00:32:31", date_occurred: "2011-08-29">
I'm using Rails 3.0.1 and Ruby 1.9.2
You need to pass the initial value for the "memo" to inject
, like:
notes.inject(0) {|sum, note| sum + note.time_spent }
精彩评论