简体   繁体   English

Rails:模型和模型模板交互

[英]Rails: Model and model template interaction

Using : Ruby on Rails (3.0.3) 使用 :Ruby on Rails(3.0.3)

I am building a website where one can perform different health and diet related calculations. 我正在建立一个可以执行与健康和饮食相关的计算的网站。

Each calculation holds different inputs: 每个计算都包含不同的输入:

Calculation 1 height, weight, waist_measurement 计算1身高,体重,腰围

Calculation 2 weight, hip_measurement 计算2重量,臀部测量

Calculation 3 hip_measurement, lifestyle_type (string), age, calory_intake 计算3臀部测量,生活方式类型(字符串),年龄,卡路里摄入量

...and so on. ...等等。

I have (as a suggestion from a friend who knows RoR) created a model "Calculation" (which stands as a template) and another model "CalculationSheet" that will be used as an instance model etc. 我(作为一个了解RoR的朋友的建议)创建了一个模型“ Calculation”(作为模板)和另一个模型“ CalculationSheet”将用作实例模型等。

Model:Calculation (the template) holds data like: - TypeOfCalculation ('health', 'lifestyle' etc) - SearchTags - isSpecial (a boolean to isolate certain special Calculations) Model:Calculation(模板)保存的数据如下:-TypeOfCalculation(“健康”,“生活方式”等)-SearchTags-isSpecial(用于隔离某些特殊计算的布尔值)

There are as many Calculation objects as there are calculation "types" on the website (such as the 3 I mentioned earlier) 网站上的计算对象与计算“类型”一样多(例如我前面提到的3个)

Model: CalculationSheet will only hold data like: - Result (such as BMI=>22) 模型:CalculationSheet将仅保存以下数据:-结果(例如BMI => 22)

A CalculationSheet will be created when a calculation is performed on the website using Create. 当使用“创建”在网站上执行计算时,将创建一个CalculationSheet。 I have no need to save these calculations (prefer not to actually). 我无需保存这些计算(最好不要实际使用)。

The most important reason for using the CalculationSheet model is to be able to validate the input ...and here is the problem. 使用CalculationSheet模型的最重要原因是能够验证输入 ...这就是问题所在。

Problem: How do I validate inputs from a model that does not hold each input as an attribute. 问题:如何验证不将每个输入都保存为属性的模型的输入。 I cannot make an attribute for each type of input (there are literally 100's), such as height, weight, life_style, waist, age etc... 我无法为每种类型的输入(实际上有100个)设置属性,例如身高,体重,生活方式,腰围,年龄等。

This is my plan as for now: 这是我目前的计划:

class CalculationSheet < ActiveRecord::Base
  belongs_to :calculation

  # BMI
  validates :height, :numericality => true, :if => :calculation_is_bmi?
  validates :weight, :numericality => true, :if => :calculation_is_bmi?

  def calculation_is_bmi?
    # do something
  end  
end

which (obviously) does not work. (显然)不起作用。 It tries to validate height which does not exist. 它尝试验证不存在的高度。

What I would like to do is to "validates params[:height], :numer... etc" but it does not seem to work either... 我想做的是“验证params [:height] 、: numer ...等”,但它似乎也不起作用...

How can I solve this? 我该如何解决?

You can use ActiveModel instead of ActiveRecord as shown in these lines: 您可以使用ActiveModel代替ActiveRecord ,如以下行所示:

class CalculationSheet
  include ActiveModel::Validations
  attr_accessor :height, :weight
  validates_numericality_of :height, :weight
end

This will allow for form validation of every attribute, but you don't store anything. 这将允许对每个属性进行表单验证,但是您不存储任何内容。 Here's a great post by Yehuda Katz about ActiveModel : ActiveModel: Make Any Ruby Object Feel Like ActiveRecord 这是Yehuda Katz撰写的有关ActiveModel的精彩文章: ActiveModel:使任何Ruby对象都感觉像ActiveRecord

For Rails 3 use validates_with to use a custom validator. 对于Rails 3,请使用validates_with来使用自定义验证器。 See this great post for information on how to use them. 有关如何使用它们的信息,请参见这篇出色的文章

edit : 编辑

How about this: 这个怎么样:

class CalculationSheet
  include ActiveModel::Validations
  attr_accessor :height, :weight, :hip_circumference
  validates_numericality_of :height, :weight, :if => :bmi?
  validates_numericality_of :height, :hip_circumference, :if => :bai?

  def bmi?
    [height, weight].all?(&:present?)
  end

  def bmi
    weight / (height**2)
  end

  def bai?
    [height, :hip_circumference].all?(&:present?)
  end

  def bai
    (hip_circumference / height**1.5) − 18
  end

end

The trick is in your comment "I have no need to save these calculations": 诀窍在于您的评论“我不需要保存这些计算”:

ActiveRecord is all about providing persistence for your data (ie saving). ActiveRecord就是为您的数据提供持久性(即保存)。 If you're just wanting to do calculations, you can use plain old ruby objects :) 如果您只想进行计算,则可以使用普通的旧红宝石对象:)

With stuff like this I'd be tempted to build a little library (perhaps in /lib now and as a gem later) that does your calculations. 使用这样的东西,我很想建立一个小的库(也许现在在/ lib中,以后再作为gem)来进行计算。

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

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