【问题标题】:Rails survey calculating results and storing results in DBRails 调查计算结果并将结果存储在数据库中
【发布时间】:2018-04-11 17:21:24
【问题描述】:

我的调查(用水)的控制器出现问题。它从一个表格中收集了 30 多个变量,这些输入需要保存到 waterusage db 并用于计算最终分数,也保存在数据库中。

class Waterusage < ApplicationRecord
  belongs_to :user
end


class WaterusagesController < ApplicationController

  def new
    @waterusage = Waterusage.new
  end

  def create
    @user = User.find(params[:user_id])
    _showerTotal = :average_shower * :shower_flow_rate * :household_size
    _bathTotal = :bath_rate * :bath_multiplier * 35
    _bathroomSinkTotal = :bathroom_sink_usage * :bathroom_sink_flow_rate * :household_size
    _toiletTotal = :mellow * :low_flow_toilet * :household_size
    _kitchenTotal = :kitchen_sink_usage * :kitchen_sink_flow_rate
    _dishwashingTotal = :dishwasher_rate * :dishwasher_multiplier * :dishwasher_method
    _laundryTotal = :laundry_rate * :laundry_method * :laundry_multiplier
    _homeUsage = _showerTotal + _bathTotal + _bathroomSinkTotal + _toiletTotal + _kitchenTotal + _dishwashingTotal + _laundryTotal + :greywater
    _lawnTotal = :lawn_rate * :lawn_multiplier * :lawn_size * :xeriscaping
    _swimmingTotal = (:swimming_pool / 365) + (:swimming_months * 1000 / 365
    _carwashTotal = :carwash_rate * :carwash_multiplier * :carwash_method
    _outsideUsage = _lawnTotal + _swimmingTotal + _carwashTotal
    _drivingTotal = 0.735 * :miles
    _powerTotal = :statewater * :percent_statewater / 100
    _indirectTotal = :shopping + :paper_recycling + :plastic_recycling + :can_recycling + :textile_recycling + :diet + (200 * :pet_cost / 30)
    :household_total = _homeUsage + _outsideUsage + _drivingTotal + _powerTotal + _indirectTotal
    :individual_total = :household_total / :household_size
    @waterusage = @user.waterusage.create(waterusage_params)
    redirect_to user_path(@user)
  end

  def destroy
    @user = User.find(params[:user_id])
    @waterusage = @user.waterusage.find(params[:id])
    @waterusage.destroy
    redirect_to user_path(@user)
  end

  private
    def waterusage_params
      params.require(:waterusage).permit(:household_size, :average_shower,
        :shower_flow_rate, :bath_rate, :bath_multiplier, :bathroom_sink_usage,
        :bathroom_sink_flow_rate, :mellow, :low_flow_toilet, :kitchen_sink_usage,
        :kitchen_sink_flow_rate, :dishwasher_rate, :dishwasher_multiplier,
        :dishwasher_method, :laundry_rate, :laundry_multiplier, :laundry_method,
        :greywater, :lawn_rate, :lawn_multiplier, :lawn_size, :xeriscaping,
        :swimming_pool, :swimming_months, :carwash_rate, :carwash_multiplier,
        :carwash_method, :miles, :statewater, :percent_statewater, :shopping,
        :paper_recycling, :plastic_recycling, :can_recycling, :textile_recycling,
        :diet, :pet_cost, :individual_total, :household_total)
    end
end

有没有更好的方法可以做到这一点?目前,在汇总小计的行上存在错误。 (即:household_total = _homeUsage + _outsideUsage + _drivingTotal + _powerTotal + _indirectTotal ) 另外我不确定我是否将用户信息正确连接到调查架构

【问题讨论】:

  • 这里有未闭合的括号吗:(:swimming_months * 1000 / 365?
  • 哦,另外,我想我会很想将所有原始变量存储在 WaterUsage 模型中,然后将所有数学运算转移到 decorator 中。
  • 这在我看来不像是有效的 Ruby 代码。您正在使用局部变量之类的符号。即使代码有效,您也不会对结果做任何事情。

标签: ruby-on-rails database activerecord


【解决方案1】:

您不想在控制器中进行数学运算。瘦控制器,胖模型。除此之外,它失败的一个原因是语法不正确。一个符号 (:hello_world) 不能被赋值,也不能包含一个值。不太重要的是,虽然有一个带下划线前缀的局部变量并不违法,但这不是 Ruby 中的约定。骆驼箱也不是。你想要 hello_world 而不是 helloWorld。总之……

假设:您要求总数必须保持不变。它们不能是计算值。

您想将这些计算转移到模型中。与其分配大量变量,不如使用方法。这样您就可以轻松地对它们进行单元测试。

这里缺少什么:模型中的验证确保所有预期的属性值都存在。控制器也应该在创建时处理无效的 Waterusage 实例。此代码未经测试,仅用于说明目的。

class Waterusage < ApplicationRecord
  belongs_to user

  before_validation :calculate_totals

  def calculate_totals
    self.household_total = get_household_total
    self.individual_total = get_individual_total
  end

  def get_household_total
    home_usage + outside_usage + driving_total + power_total + indirect_total
  end

  def get_individual_total
    household_total / household_size
  end

  def home_usage
    shower_total + bath_total + bathroom_sink_total + toilet_total + kitchen_total + dishwashing_total + laundry_total + greywater
  end

  def outside_usage
    lawn_total + swimming_total + carwash_total
  end

  def driving_total
    0.735 * miles
  end

  def power_total
    statewater * percent_statewater / 100
  end

  def indirect_total
    shopping + paper_recycling + plastic_recycling + can_recycling + textile_recycling + diet + (200 * pet_cost / 30)
  end

  def shower_total
    average_shower * shower_flow_rate * household_size
  end

  def bath_total
    bath_rate * bath_multiplier * 35
  end

  def bathroom_sink_total
     bathroom_sink_usage * bathroom_sink_flow_rate * household_size
  end

  def toilet_total
    mellow * low_flow_toilet * household_size
  end

  def kitchen_total
    kitchen_sink_usage * kitchen_sink_flow_rate
  end

  def dishwashing_total
    dishwasher_rate * dishwasher_multiplier * dishwasher_method
  end

  def laundry_total
    laundry_rate * laundry_method * laundry_multiplier
  end

  def lawn_total
    lawn_rate * lawn_multiplier * lawn_size * xeriscaping
  end

  def swimming_total
    (swimming_pool / 365) + (swimming_months * 1000 / 365)
  end

  def carwash_total
    carwash_rate * carwash_multiplier * carwash_method
  end
end

class WaterusagesController < ApplicationController
  ...

  def create
    @user = User.find(params[:user_id])
    @waterusage = @user.waterusage.create(waterusage_params)
    redirect_to user_path(@user)
  end

  ...

end

【讨论】:

    【解决方案2】:

    首先在 create 中的每个 ':' 前加上 'params[' 和后缀 ']',然后用 '@' 更改每个 '_'。

    应该是这样的:

     _powerTotal = :statewater * :percent_statewater / 100 
    

    变成

     @powerTotal = params[:statewater].to_i * params[:percent_statewater].to_i /100
    

    这样,

     :individual_total = :household_total / :household_size  
    

    变成

     @individual_total = params[:household_total].to_i / params[:household_size].to_i
    

    此外,您的计算什么也不做,它们只是四处飘荡,事实上,您甚至无法从您的视图中调用它们。

    如果您希望它保存在与用户相关的水使用对象上,它会是 individual_total 属性;

    @waterusage = @user.waterusage.create(waterusage_params, individual_total: @individual_total).
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-13
      • 1970-01-01
      • 2017-08-28
      • 2021-10-21
      • 2022-08-06
      • 1970-01-01
      • 2017-09-08
      • 2015-11-05
      相关资源
      最近更新 更多