pomiń pewną metodę walidacji w modelu

Używam Rails v2.3

Jeśli mam 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

Jak widzisz powyżej, mam 3 niestandardowe metody walidacji i używam ich do walidacji modelu.

Jeśli mam inną metodę w tej klasie modelu, która zapisuje nową instancję modelu w następujący sposób:

# "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

Chciałbym pominąć walidację method_2 w tej konkretnej metodzie Oszczędzania nowego samochodu, Jak pominąć pewną metodę walidacji?

Author: Leem.fin, 2012-01-16

4 answers

Zaktualizuj swój model do tego

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

Następnie w kontrolerze wpisz:

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

Jeśli otrzymujesz :flag poprzez zmienną params w kontrolerze, możesz użyć

def save_special_car
   new_car=Car.new(skip_method_2: params[:flag].present?)
   new_car.save
end
 52
Author: shime,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-29 17:05:16

Podstawowe użycie walidacji warunkowej to:

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

Sprawdź dokumenty, aby uzyskać więcej szczegółów.

Dopasowanie go do twojego 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
 11
Author: Michał Szajbe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-01-16 15:28:35

Użyj bloku w swojej walidacji coś w stylu:

validates_presence_of :your_field, :if =>  lambda{|e| e.your_flag ...your condition}
 0
Author: Ravindra,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-12 12:14:37

W zależności od tego, czy flaga pogody jest true, czy false, użyj metody save(false), aby pominąć walidację.

 -2
Author: Wahaj Ali,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-01-16 15:09:07