errors.add(attr, "is Chad") if value == "Chad"
end
end
APPENDIX ?– ACTIVE RECORD METHODS IN DETAIL 259
The :if option specifies a method, proc, or string that is called in order to determine
whether the validation should occur at all:
class Account < ActiveRecord::Base
validates_each(:first_name,
:last_name,
:if => :check_each) do |record, attr, value|
record.errors.add(attr, "is Chad") if value == "Chad"
end
end
class Account < ActiveRecord::Base
validates_each(:first_name,
:last_name,
:if => "check_each") do |record, attr, value|
record.errors.add(attr, "is Chad") if value == "Chad"
end
end
class Account < ActiveRecord::Base
validates_each(:first_name,
:last_name,
:if => Proc.new {|a| a.create_step == 2 }) do |record, attr, value|
record.errors.add(attr, "is Chad") if value == "Chad"
end
end
For more information regarding validations, see Chapter 4.
validates_exclusion_of(*attribute_names)
This method adds errors to the specified attributes if the attributes values appear in the specified
enumerable object:
class Account < ActiveRecord::Base
validates_exclusion_of(:first_name, :in => %w(Chad Jon Kevin))
end
In addition to the attributes to validate, this method takes the configuration options of
:in, :message, :allow_nil, and :if.
Pages:
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565