简体   繁体   English

如何在Ruby on Rails中创建一种方法来计算每英里分钟

[英]How to create a method to calculate minutes per mile in Ruby on Rails

In my Rails 4.2.6 / Ruby 2.2.4 project, I have a cardio exercise model with duration, distance and average pace fields. 在我的Rails 4.2.6 / Ruby 2.2.4项目中,我有一个有氧运动模型,其中包含持续时间,距离和平均步速字段。 "Duration" and "average pace" are time datatypes; “持续时间”和“平均速度”是时间数据类型; "distance" is a decimal datatype. “距离”是十进制数据类型。 I need help writing a method to calculate a user's average pace (minutes per mile) based on a user's inputs in the duration and distance fields. 我需要帮助来编写一种方法,该方法可以根据持续时间和距离字段中用户的输入来计算用户的平均步速(每英里分钟)。

* EDIT * *编辑*

The duration and distance fields in my cardio exercises form (_form.html.erb): 我的有氧运动形式(_form.html.erb)中的持续时间和距离字段:

  <div class="cardio_duration">
    <%= f.input :duration do %> <br>   
    <%= f.time_select :duration, :include_blank => true, include_seconds: true %>
    <% end %>
  </div>

  <div class="cardio-time"> 
    <%= f.input :distance, :include_blank => true %>
    <% end %>
 </div>

Here's what I have coded so far in my cardio exercise model: 到目前为止,这是我在有氧运动模型中编写的代码:

before_save { self.average_pace = self.calculate_average_pace } # calls calculate average pace method

I understand that when calculating minutes per mile, the time factor is on top in the equation. 我了解到,在计算每英里分钟数时,时间因素是最重要的因素。 Here's the method I've written: 这是我编写的方法:

def calculate_average_pace
    duration = self.duration
    distance = self.distance
    average_pace = duration / distance
end

When I execute this method, it generates the following error: 当我执行此方法时,它会产生以下错误:

undefined method `/' for 2000-01-01 16:50:00 UTC:Time

I'm trying to do something like this: 26 minutes / 1.17 miles, which equals 22.29 minutes per mile. 我正在尝试执行以下操作:26分钟/ 1.17英里,等于每英里22.29分钟。 How should I change my code to fix the problem? 我应该如何更改代码以解决此问题? Thanks for the help & explanation. 感谢您的帮助和解释。

**EDIT ** **编辑**

This is the relevant DB schema: 这是相关的数据库模式:

  create_table "cardio_exercises", force: :cascade do |t|
    ...
    t.datetime "created_at"
    t.datetime "updated_at"
    t.time     "duration"
    t.decimal  "distance"
    t.time     "average_pace"
  end

You're getting undefined_method error because you're trying to divide Time by Decimal . 您正在收到undefined_method错误,因为您试图将Time除以Decimal Only Numeric values can be divided. 只能对数值进行除法。

I tried to achive that functionality like this. 我试图达到这种功能。

# _form.html
# ...
  <div class="field">
    <%= f.label :duration %><br>
    <%= f.time_select :duration, include_blank: false, include_seconds: true %>
  </div>
# ...

# Cardio model
class Cardio < ActiveRecord::Base
  before_save :set_mpm

  def set_mpm
    # find minutes from time entered by user
    mins = duration.hour * 60 + duration.min + duration.sec / 60
    self.mpm = mins / distance
  end
end

# controller
  def create
    @cardio = Cardio.new(cardio_params)

    respond_to do |format|
      if @cardio.save
        format.html { redirect_to @cardio, notice: 'Cardio was successfully created.' }
        format.json { render action: 'show', status: :created, location: @cardio }
      else
        format.html { render action: 'new' }
        format.json { render json: @cardio.errors, status: :unprocessable_entity }
      end
    end
  end

Here is the result 这是结果

结果

Is that works for you? 那对你有用吗?

UPDATE 更新

1 hour for 10 miles works for me. 1小时10英里对我有效。

结果2

Here is my DB schema. 这是我的数据库架构。

  create_table "cardios", force: :cascade do |t|
    t.time     "duration"
    t.decimal  "distance"
    t.decimal  "mpm"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on Rails运行极其缓慢(每个命令5-10分钟) - Ruby on Rails runs extremely slowly (5-10 minutes per command) 不确定创建方法的工作原理-Ruby on Rails - Unsure how create method works - ruby on rails 用于计算百分位数的Ruby on Rails方法 - 可以重构吗? - Ruby on Rails method to calculate percentiles - can it be refactored? 在 Rails 上的 Ruby 中创建方法问题 - Create method problem in Ruby on Rails 如何创建类似于javascript节流/反跳功能的Rails / Ruby方法 - How to create a Rails/Ruby method similar to javascript throttle/debounce function 如何使用 Grape 和 MongoDB 在 Ruby on Rails 中正确测试“post”(创建)方法 - How to properly test a 'post' (create) method in Ruby on Rails with Grape and MongoDB 如何在控制器中创建一个方法,以便在轨道上的ruby视图中调用IF条件? - How to create a method in controller for the IF condition to call in the view in ruby on rails? Ruby on Rails:如何创建“ recent_search_path”帮助器方法? - Ruby on Rails: How to create a “recent_search_path” helper method? 如何计算红宝石在轨道上的嵌套字段的百分比 - how to calculate percentage for nested fields in ruby on rails Ruby / Rails如何计算和舍入价格? - Ruby/Rails How to Calculate and Round Prices?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM