Prev | Current Page 561 | 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_presence_of(*attribute_names)
This validation method adds errors to the specified attributes whose values are blank:
class Account < ActiveRecord::Base
validates_presence_of :first_name
end
For association attributes, it is important to validate the presence of the foreign key, not
the object itself:
validates_presence_of :site # Incorrect
validates_presence_of :site_id # Correct
In addition to the attributes to validate, this method takes the configuration options of
:on, :message, and :if.
APPENDIX ?–  ACTIVE RECORD METHODS IN DETAIL 264
The :on option specifies for what methods this validation should be active. Valid options
are :save, :create, and :update. The default value is :save.
validates_presence_of(:first_name, :on => :create)
The :message option provides a custom error message. The default error message is
"can't be blank".
validates_presence_of(:first_name, :message => "must be provided")
The :if option specifies a method, proc, or string that is called in order to determine
whether the validation should occur at all:
validates_presence_of(:first_name, :if => :check_first_name)
validates_presence_of(:first_name, :if => "check_first_name")
validates_presence_of(:first_name, :if => Proc.


Pages:
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573