Prev | Current Page 552 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

new {|a| a.create_step == 2 }
For more information regarding validations, see Chapter 4.
validates_each(*attribute_names) { |record, attribute, value| . . . }
This method validates each of the specified attributes against the specified block:
class Account < ActiveRecord::Base
validates_each(:first_name, :last_name) do |record, attribute, value|
record.errors.add(attribute, "is Chad") if value == "Chad"
end
end
In addition to the attributes to validate, this method takes the configuration options of
:on, :allow_nil, and :if.
The :on option specifies for what methods this validation should be active. Valid options
are :save, :create, and :update. The default value is :save.
class Account < ActiveRecord::Base
validates_each(:first_name, :last_name, :on => :create) do |record, attr, value|
record.errors.add(attr, "is Chad") if value == "Chad"
end
end
The :allow_nil option, if true, specifies that that the validation should be skipped if the
attribute is nil:
class Account < ActiveRecord::Base
validates_each(:first_name, :last_name, :allow_nil => true) do |record, attr, value|
record.


Pages:
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564