Ruby On Rails Lesson
We’ve constrained our User model to require a name for each user, but we should go further: The users’ names will be displayed on the sample site, so we should enforce some limit on their length. With all the work we did in Section 6.2.2, this step is easy. We start with a test. There’s no science to picking a maximum length; we’ll just pull 50 out of thin air as a reasonable upper bound, which means verifying that names of 51 characters are too long (Listing 6.14).
For convenience, we’ve used ‘‘string multiplication’’ in Listing 6.14 to make a string 51 characters long. We can see how this works using the console:
The test in Listing 6.14 should fail. To get it to pass, we need to know about the validation argument to constrain length, :length, along with the :maximum parameter to enforce the upper bound (Listing 6.15).
Now the tests should pass. With our test suite passing again, we can move on to a more challenging validation: email format.