I am trying to show a sum of the floats of all records entered, and I can't seem to figure out how to do it. Here's the situation. My app (purely for learning purposes) has a user who can register a swimmer profile and upon doing so, can record swims for the swimmer. Swim belongs to Swimmer and Swimmer has many swims.
// swims_controller.rb
// SwimsController#index
@swims = @swimmer.swims.all
@distances = @swimmer.swims.all(:select => "distance")
In the view for this action, I iterate through swims to get swim.date and swim.distance. Then I add this line which doesn't work
<p>Total distance: <%= @distances.each { | x | x.distance.inject(:+).to_f } %></p>
The error I am getting is "undefined method `inject' for 5500.0:Float" (5500.0 is the distance of the first swim recorded for this swimmer). I used inject based on other threads (e.g., How to sum array of numbers in Ruby?) but clearly, I'm missing something. Actually, I'm probably missing quite a lot because I'd like to put this a #total_distance method in the Swim model (swim.rb) but when I try to do that, I can't get the controller to recognize the method and I read elsewhere that a view should not try to access a method defined in a model due to "caching" issues.
Anyone's useful guidance on this would greatly help. Seems like a pretty basic thing to want to do but for some reason, I find scant guidance on a question like this after numerous search queries. Thanks!