在模型或控制器中计算

时间:2016-06-01 19:58:58

标签: ruby-on-rails

我正在建立一个减肥应用程序。对于我的应用中的每个用户has_one :profilehas_many :weights。每个个人资料belongs_to :pal。为了让我的应用程序工作,我需要一个名为SMR的值,它基本上是一个公式,它将用户的大小,年龄和性别(来自配置文件表),用户的当前权重(来自权重表)以及来自朋友桌。

我可以在profiles_controller.rb show动作中计算SMR并将其显示在配置文件show.html.erb中。

我现在有两个问题:

  1. profiles_controller.rb show动作中进行此计算是否正确,还是应该在profile.rb模型中进行此计算?如果我应该在模型中这样做:我该怎么做(代码应该怎么样)?
  2. 我稍后会在我的应用中将SMR值作为其他计算的变量。我怎样才能实现这一点(如果它在配置文件控制器/模型中计算,但稍后需要在其他地方使用)?
  3. 我对Rails世界相当新,所以也许我的问题真的是noob问题。

    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
    

2 个答案:

答案 0 :(得分:5)

我认为您应该创建一个单独的类,或者您也可以在个人资料模型上创建

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

在你的控制器show方法中调用这个类:

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

将此类添加为模型文件夹

中的smr_calculator.rb

因此,在应用程序的任何位置,您都需要@smr,您可以使用个人资料和当前用户调用此类

答案 1 :(得分:0)

您可以在services文件夹中创建一个app目录。在其中,您可以将类创建为CalculatorService

示例:

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

  def smr_value
    @pal = @profile.pal
    @goal = @profile.goal
    @current_weight = Weight.users_weight(@user.id)

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

 class Weight < ActiveRecord::Base
   scope :users_weight, ->(user_id) { where(:user_id => user_id).order(:day).last}
end

然后像这样在您的控制器中调用此服务:

@smr_calculator = CalculatorService.new(@profile, current_user)
@smr = @smr_calculator.smr_value 
相关问题