Ruby-on-rails 跳过模型中的某些验证方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8881712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:39:26  来源:igfitidea点击:

skip certain validation method in Model

ruby-on-railsruby-on-rails-3ruby-on-rails-3.1

提问by Leem.fin

I am using Rails v2.3

我正在使用Rails v2.3

If I have a model:

如果我有一个模型

class car < ActiveRecord::Base

  validate :method_1, :method_2, :method_3

  ...
  # custom validation methods
  def method_1
    ...
  end

  def method_2
    ...
  end

  def method_3
    ...
  end
end

As you see above, I have 3 custom validation methods, and I use them for model validation.

正如您在上面看到的,我有3 个自定义验证方法,我将它们用于模型验证。

If I have another method in this model class which save an new instance of the model like following:

如果我在这个模型类中有另一种方法来保存模型的新实例,如下所示:

# "flag" here is NOT a DB based attribute
def save_special_car flag
   new_car=Car.new(...)

   new_car.save #how to skip validation method_2 if flag==true
end

I would like to skip the validation of method_2in this particular method for saving new car, how to skip the certain validation method?

我想跳过method_2这个保存新车的特定方法的验证,如何跳过某些验证方法?

回答by shime

Update your model to this

将您的模型更新为此

class Car < ActiveRecord::Base

  # depending on how you deal with mass-assignment
  # protection in newer Rails versions,
  # you might want to uncomment this line
  # 
  # attr_accessible :skip_method_2

  attr_accessor :skip_method_2 

  validate :method_1, :method_3
  validate :method_2, unless: :skip_method_2

  private # encapsulation is cool, so we are cool

    # custom validation methods
    def method_1
      # ...
    end

    def method_2
      # ...
    end

    def method_3
      # ...
    end
end

Then in your controller put:

然后在您的控制器中输入:

def save_special_car
   new_car=Car.new(skip_method_2: true)
   new_car.save
end

If you're getting :flagvia params variable in your controller, you can use

如果您:flag通过控制器中的 params 变量获取,则可以使用

def save_special_car
   new_car=Car.new(skip_method_2: params[:flag].present?)
   new_car.save
end

回答by Micha? Szajbe

The basic usage of conditional validation is:

条件验证的基本用法是:

class Car < ActiveRecord::Base

  validate :method_1
  validate :method_2, :if => :perform_validation?
  validate :method_3, :unless => :skip_validation?

  def perform_validation?
    # check some condition
  end

  def skip_validation?
    # check some condition
  end

  # ... actual validation methods omitted
end

Check out the docs for more details.

查看文档以获取更多详细信息。

Adjusting it to your screnario:

将其调整为您的场景:

class Car < ActiveRecord::Base

  validate :method_1, :method_3
  validate :method_2, :unless => :flag?

  attr_accessor :flag

  def flag?
    @flag
  end    

  # ... actual validation methods omitted
end

car = Car.new(...)
car.flag = true
car.save

回答by Ravindra

Use block in your validation something like :

在您的验证中使用块,例如:

validates_presence_of :your_field, :if =>  lambda{|e| e.your_flag ...your condition}

回答by Wahaj Ali

Depending on weather flag is true of false, use the method save(false)to skip validation.

根据天气标志是真还是假,使用该方法save(false)跳过验证。