本文介绍了在模型或控制器中计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款减肥应用.为此,在我的应用程序中,每个用户 has_one :profilehas_many :weights.每个配置文件belongs_to :pal.为了让我的应用程序正常工作,我需要一个名为 SMR 的值,它基本上是一个公式,它将用户的大小、年龄和性别(均来自配置文件表)、用户当前的体重(来自权重表)以及来自的浮点数作为变量朋友桌.

I'm builing a weight loss app. For this in my app each user has_one :profile and has_many :weights. Each profile belongs_to :pal. For my app to work I need a value called SMR which basically is a formula that takes as variables the user's size, age and gender (all from profiles table), the user's current weight (from weights table) as well as a float number from pal table.

我能够在 profiles_controller.rb 显示操作中计算 SMR,并将其显示在配置文件 show.html.erb 中.

I am able to calculate SMR in profiles_controller.rb show action and show it in the profiles show.html.erb.

我现在有两个问题:

  1. profiles_controller.rb 显示操作中进行此计算是否正确,还是应该在 profile.rb 模型中进行计算?如果我应该在模型中做:我该怎么做(代码应该是什么样子)?
  2. 稍后我将需要在我的应用程序中使用 SMR 值作为其他计算的变量.我如何才能实现这一点(如果它是在配置文件控制器/模型中计算的,但稍后需要在其他地方使用)?
  1. Is it correct to do this calculation in the profiles_controller.rb show action or should I do it in the profile.rb model? If I should do it in the model: how can I do it (how should the code look like)?
  2. I will need the SMR value later on in my app as a variable for other calculations as well. How can I achieve this (if it is calculated in the profile controller/model but needed somewhere else later on)?

我对 Rails 世界还很陌生,所以也许我的问题真的是菜鸟问题.

I'm fairly new to the Rails world so maybe my questions are really noob questions.

profile.rb

class Profile < ActiveRecord::Base
  belongs_to :user
  belongs_to :pal
  belongs_to :goal


  def age
    if birthdate != nil
      now = Time.now.utc.to_date
      now.year - birthdate.year - (birthdate.to_date.change(:year => now.year) > now ? 1 : 0)
    else
      nil
    end
  end
end

weight.rb

class Weight < ActiveRecord::Base
  belongs_to :user
end

pal.rb

class Pal < ActiveRecord::Base
  has_many :profiles
end

profiles_controller.rb(仅显示操作)

  def show
    @pal = @profile.pal
    @goal = @profile.goal
    @current_weight = Weight.where(:user_id => current_user.id).order(:day).last

    if @profile.gender == 0
      @smr = (10*@current_weight.kilograms+6.25*@profile.size-5*@profile.age+5)*@pal.value
    elsif @profile.gender == 1
      @smr = (10*@current_weight.kilograms+6.25*@profile.size-5*@profile.age-161)*@pal.value
    else
      nil
    end
  end

推荐答案

我认为你应该创建一个单独的类,或者你也可以在配置文件模型上做

I think you should create a separate class or you can do on profile model as well

class SmrCalculator
  def initialize(profile, user)
     @profile = profile
     @user = user
  end

  def get_smr
    @pal = @profile.pal
    @goal = @profile.goal
    @current_weight = Weight.where(:user_id => @user.id).order(:day).last

    if @profile.gender == 0
      @smr = (10*@current_weight.kilograms+6.25*@profile.size-5*@profile.age+5)*@pal.value
    elsif @profile.gender == 1
      @smr = (10*@current_weight.kilograms+6.25*@profile.size-5*@profile.age-161)*@pal.value
    else
      nil
    end
  @smr
  end

end

并在您的控制器显示方法上调用此类,如下所示:

And call this class on your controller show method like this:

@smr_calculator = SmrCalculator.new(@profile, current_user)
@smr = @smr_calculator.get_smr

并在模型文件夹中将此类添加为 smr_calculator.rb

And add this class as smr_calculator.rb in models folder

所以在你需要@smr 的应用程序的任何地方,你都可以使用个人资料和当前用户调用这个类

so anywhere in the app you need @smr you can call this class with profile and current user

这篇关于在模型或控制器中计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:21