Prev | Current Page 556 | 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.
APPENDIX ?–  ACTIVE RECORD METHODS IN DETAIL 261
validates_inclusion_of(*attribute_names)
This method adds errors to the specified attributes if the attribute??™s values do not appear in
the specified enumerable object:
class Account < ActiveRecord::Base
validates_inclusion_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.
The :in option specifies an enumerable object against which the attribute values should
be checked:
validates_inclusion_of(:first_name, :in => %w(Chad Jon Kevin))
The :message option provides a custom error message. The default error message is "is
not included in the list".
validates_inclusion_of(:first_name,
:in => %w(Chad Jon Kevin),
:message => "must be one of the authors' names")
The :allow_nil option, if true, specifies that that the validation should be skipped if the
attribute is nil:
validates_inclusion_of(:first_name, :in => %w(Chad Jon Kevin), :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_inclusion_of(:first_name, :in => %w(Chad Jon Kevin), :if => :check_name)
validates_inclusion_of(:first_name, :in => %w(Chad Jon Kevin), :if => "check_name")
validates_inclusion_of(:first_name,
:in => %w(Chad Jon Kevin),
:if => Proc.


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