简体   繁体   English

如何在Rails的计算中使用枚举数据库值(数字)?

[英]How do I use enum database values (numeric) in calculations in Rails?

In my app I have a model that has 2 different enum parameters associated with it. 在我的应用程序中,我有一个模型,该模型具有与之关联的2个不同的枚举参数。 I want to use these parameters in a calculation (essentially taking the database numerical value of each and adding them together) and return a result. 我想在计算中使用这些参数(基本上是将每个参数的数据库数字值加在一起)并返回结果。 Ideally I want to iterate through these model objects, sum their enum values and use the result to provide a score to the parent object. 理想情况下,我想遍历这些模型对象,求和它们的枚举值,并使用结果为父对象提供一个分数。

Here is my model - it's for a SWOT analysis: 这是我的模型-用于SWOT分析:

class Swot < ActiveRecord::Base
belongs_to :sales_opportunity, inverse_of: :swots
validates :swot_details, presence: true
validates :sales_opportunity_id, presence: true
enum swot_type: [:strength, :weakness, :opportunity, :threat]
enum swot_importance: [:minimal, :marginal, :noteworthy, :significant, :critical]
before_save :update_opportunity_score

def update_opportunity_score
    sales_opportunity.update_swot_score
    sales_opportunity.save
end
end

I'm trying to write the code for the update_swot_score function but I'm totally lost as to how to achieve it. 我正在尝试为update_swot_score函数编写代码,但是我完全不知道如何实现它。 What I need is the ability to extract all swots that are "strength" and sum the swot_importance values (1 for "minimal, 2 for "marginal"... 5 for "critical"), then do the same with "weakness", "opportunity" and "threat" before using the summed score in a calculation. 我需要的是能够提取所有“强度”的swot并求和swot_importance值(1表示“最小值”,2表示“ marginal” ... 5表示“ critical”),然后对“ weakness”进行相同处理的能力,在计算中使用总分之前,先说“机会”和“威胁”。

I've been playing around with the code below, but now I'm just totally lost as to what I'm doing. 我一直在玩下面的代码,但是现在我对自己的工作完全迷失了。 Any help would be appreciated. 任何帮助,将不胜感激。

def update_swot_score
    strength = SalesOpportunity.swots.where("swot_type <> ?", Swot.swot_types[:strength]).each do |strong|
        SalesOpportunity.swots.swot_importances[strong.swot_importance]
end
end

Rails enums provide a helpful array of options and integer values. Rails枚举提供了一个有用的选项和整数值数组。 In rails console, run Swot.swot_importances and see. 在Rails控制台中,运行Swot.swot_importances并查看。 So, you can just lookup from that array: 因此,您可以只从该数组中查找:

class Swot < ActiveRecord::Base
  belongs_to :sales_opportunity, inverse_of: :swots
  validates :swot_details, presence: true
  validates :sales_opportunity_id, presence: true
  enum swot_type: { strength: 1, weakness: 2, opportunity: 3, threat: 4 }
  enum swot_importance: { minimal: 1, marginal: 2, noteworthy: 3,
                          significant: 4, critical: 5 }
  before_save :update_opportunity_score

  def update_opportunity_score
    sales_opportunity = swot_score
  end

  def swot_score
    Swot.swot_types[swot_type] + Swot.swot_importances[swot_importance]
  end
end

Note that there's no need to specify self. 请注意,无需指定self. in a model. 在模型中。 Also, you always want to use save! 另外,您始终想使用save! unless you're checking return values since save can fail. 除非您要检查返回值,否则save可能会失败。 But saving in a before callback is redundant. 但是保存在before回调中是多余的。 Also, you should specify the integer values of your enums explicitly since you're relying on them for business logic. 另外,您应该显式指定枚举的整数值,因为您将其用于业务逻辑。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM