Rails 3验证模型失败

时间:2012-12-04 17:28:45

标签: ruby ruby-on-rails-3 validation

我在task.rb模型中定义了validate_completion方法。伪逻辑应该是

if no hazards exist
  date_completed = Date.today
elsif any hazard exists with a risk_total > 1000
  date_completed = nil
else any hazard that exists has a risk_total =< 1000 
  date_complete = Date.today
end

代码如下所示。

    def validate_completion
if self.date_completed.nil?
  if self.Biohaz_exist == "No"
    if self.Hazmat_exist == "No"
      if self.Fire_exist == "No"
        if self.Hazengy_exist == "No"
          if self.Tool_exist == "No"
            if self.Rad_exist == "No"
              if self.Laz_exist == "No"
                if self.Mag_exist == "No"
                  if self.Ergo_exist == "No"
                    if self.Mechand_exist == "No"
                      if self.Road_exist == "No"
                        if self.Fall_exist == "No"
                          if self.Hazatm_exist == "No"
                            if self.Noise_exist == "No"
                              if self.Ovrhead_exist == "No"
                                if self.Cut_exist == "No"
                                  if self.Temp_exist == "No"
                                    if self.Access_exist == "No"
                                      if self.Cowrkr_exist == "No"
                                        if self.Lonewrkr_exist == "No"
                                          self.date_completed = Date.today
                                        elsif self.Biohaz_exist == "Yes" and self.Biohaz.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Hazmat_exist == "Yes" and self.Hazmat.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Fire_exist == "Yes" and self.Fire.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Hazengy_exist == "Yes" and self.Hazengy.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Tool_exist == "Yes" and self.Tool.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Rad_exist == "Yes" and self.Rad.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Laz_exist == "Yes" and self.Laz.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Mag_exist == "Yes" and self.Mag.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Ergo_exist == "Yes" and self.Ergo.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Mechand_exist == "Yes" and self.Ergo.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Road_exist == "Yes" and self.Ergo.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Fall_exist == "Yes" and self.Fall.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Hazatm_exist == "Yes" and self.Hazatm.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Noise_exist == "Yes" and self.Noise.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Ovrhead_exist == "Yes" and self.Ovrhead.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Cut_exist == "Yes" and self.Cut.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Temp_exist == "Yes" and self.Temp.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Access_exist == "Yes" and self.Access.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Cowrkr_exist == "Yes" and self.Cowrk.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Lonewrkr_exist == "Yes" and self.Lonewrkr.risk_total > 1000
                                          self.date_completed = nil                                                                                  
                                        else
                                          self.date_completed = Date.today  
                                        end
                                      end
                                    end
                                  end
                                end
                              end
                            end
                          end
                        end
                      end
                    end
                  end
                end
              end
            end
          end
        end
      end
    end
  end
end

现在如果没有危险,它成功地将日期设置为今天。如果我然后添加一个危险它有效地将日期切换回零。但是,如果存在一种危险且风险总数低于1000,则日期仍为零。

1 个答案:

答案 0 :(得分:1)

“嗯,鉴于您的危害是静态定义的而不是数据库,您可以这样做:

#definition of all the hazard
hazards = [:Fire, :Road, :Fall]
def validate_completion
  # find all the hazards that exist
  exists = hazards.select { |hazard| self.send("#{hazard.to_s}_exist") } 
  # find if at least one high risk hazard exists
  high_risk_total = exists.detect {|hazard| self.send("#{hazard.to_s}_risk_total") > 1000 }
  # find if at least one low rick hazard exists
  low_risk_total = exists.detect {|hazard| self.send("#{hazard.to_s}_risk_total") <= 1000 }

  # validation logic here
end

您可以使用此逻辑,但可能更容易使您的危险成为数据库表并使用ActiveRecord关联和查询来简化此操作。