CHAPTER 4 ?– CORE FEATURES OF ACTIVE RECORD 86
validates_format_of
Use validates_format_of to ensure that the value of the specified attribute adheres to a regular
expression. The following example ensures that the account_name contains only uppercase
or only lowercase letters:
Class Account < ActiveRecord::Base
validates_format_of :Account_Name, :with => /[a-zA-z]+/
end
The :with option is required and must contain a regular expression.
validates_inclusion_of
Use validates_inclusion_of to ensure that the value of the specified attribute is within a supplied
enumerable object, such as an array. The following example once again checks that the
submitted account_name is "Kevin":
Class Account < ActiveRecord::Base
validates_inclusion_of :Account_Name, :in => ["Kevin"]
end
The :in option allows you to specify the enumerable object that is searched and used for
the comparison. The other options available to this method are :message, :allow_nil, and :if,
which operate as explained in previous methods.
validates_exclusion_of
The validate_exclusion method ensures that the value of the specified attribute is not within
a supplied enumerable object, such as an array.
Pages:
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224