Prev | Current Page 559 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

Valid options
are :save, :create, and :update. The default value is :save.
validates_length_of(:first_name, :maximum => 35, :on => :create)
The :allow_nil option, if true, specifies that that the validation should be skipped if the
attribute is nil:
validates_length_of(:first_name, :maximum => 35, :allow_nil => true)
The :if option specifies a method, proc, or string that is called in order to determine
whether the validation should occur at all:
validates_length_of(:first_name, :maximum => 35, :if => :check_name)
validates_length_of(:first_name, :maximum => 35, :if => "check_name")
validates_length_of(:first_name,
:maximum => 35,
:if => Proc.new {|a| a.create_step == 2 })
For more information regarding validations, see Chapter 4.
validates_numericality_of(*attribute_names)
This method adds errors to the specified attributes if the value of the attribute is not numeric.
By default, this is done by attempting to convert it to a float with Kernel.Float, but this can be
overridden with the :only_integer options.
APPENDIX ?–  ACTIVE RECORD METHODS IN DETAIL 263
class Account < ActiveRecord::Base
validates_numericality_of :value
end
In addition to the attributes to validate, this method takes the configuration options of
:only_integer, :on, :message, :allow_nil, and :if.


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