I am trying to set :total_time to the difference between 2 datetime items (:start_time and :end_time) in seconds. I can get the value that I want in the view by doing the following
<%= (@result.end_time.minus_with_coercion(@result.start_time)).round %>
but now I need to populate :total_time with this value when creating @result in the controller - I have tried the following:
def finish
@a_hill = AHill.find(params[:hill_id])
@finish_time = Result.create(:user_id => params[:user_id], :a_hill_id => params[:hill_id], :start_time =>@a_hill.start_time, :end_time=>Time.now, :total_time=>(Time.now.minus_with_coercion(@a_hill.start_time)).round)
if @finish_time.save
redirect_to(:action => "race", :id =>params[:hill_id] , :hill_id =>params[:hill_id])
end
end
or to focus on the main part of that:
:total_time=>(Time.now.minus_with_coercion(@a_hill.start_time)).round
But i get the error
"TypeError in AHillsController#finish - can't convert String into an exact number"
:total_time is an integer :start_time and :end_time are datetimes
I'm pretty new to Rails so sorry if the answer is something very obvious and thanks in advance.
What you need is to transform @a_hill 's start_time to a Datetime object try @a_hill.start_time.to_datetime
or even better change the AHill object to have datetime objects in the start_time attribute.