Prev | Current Page 200 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

But how do Active Record validations
actually work?
Implementing Validations
What are the options for implementing data validations? As you probably guessed, there are
a couple of approaches you can take:
??? You can use Active Record validations in your models and Active Record class files. Since
this is a book about Active Record, you can probably guess that this is our suggested
route. By putting your data validation code into your Active Record models, you get all
the benefits outlined in this chapter with very little downside (however, there is a little
bit of a downside, and we??™ll cover the pros and cons in a minute).
??? You can use custom code throughout your applications (generally in your controller methods
and client-side code such as JavaScript). In some situations, this will meet your needs
just fine; for example, perhaps you want to check that a field contains a certain data value
only in certain situations within your controller, or you??™re just developing a quick and dirty
unit test. However, outside of the most trivial situations, it makes more sense to centralize
the validation code into your model so that any instance of the Active Record object can
take advantage of it.


Pages:
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212