Introducing test/unit

Before we go to playing around with the model, let's make sure that it checks for unique user names and email addresses. We could just add validations to our model, then toy around in script/console to make sure that the proper errors are getting set up, but that gets old quickly, and it also doesn't do much for making sure that the behavior is maintained over time.

Instead, this time around we're going to write some unit tests. This is an automated way of making sure our expectations are being met, and once you get used to it, you'll never want to code without them. Since Rails apps dedicate a whole database to testing, we might as well make use of it.

Rails is designed to be test-driven, so it includes plenty of facilities to help you along. The first one we will need is fixtures, which allow you to set up some sample data to work against.

We will start with just one user, only filling in the essentials:

test/fixtures/users.yml

one:
id: 1
username: sandal
email: gregory.t.brown@gnospammail.com

We are going to want to check to see that a user needs to have a unique username and email address to be considered a valid record. To check this, we need three different cases.

That might look a little imposing at first, but if you look at it, we're really just codifying our expectations. We haven't added the validations yet, so we should expect these tests to fail, and they do, giving us a helpful message (truncated for clarity):

This is a bit of an oversimplified example, but hopefully you're already feeling the confidence boost of having tests covering your expectations. Most good Rails programmers practice Test Driven Development, so it is a good habit to get into. Also, this is only scratching the surface of the kind of testing you can do, but since that's an entire topic on it's own, this will have to suffice for now.

Adding Entries to Users

Now that we have a User model, we can hook up Entry to it. The type of association we're dealing with for this particular relationship is one-to-many. Any given user will have many entries, but any given entry will belong to exactly one user. The relevant keywords for this in Rails are has_many and belongs_to.

I promised the code would be crazy easy, and it is. Here are the updated definitions for Entry and User after adding the relationship, note that it's a total change of two lines of code:

There is one small change we need to make to our database schema before this will work. Most of the grunt work is handled by Rails conventions, but our Entry records need to have a place to store the user's database ID for this all to work. We can add a column for this with a simple migration.

When you run rake db:migrate it should add the user_id column, and we've now successfully hooked up User to Entry. Here is a quick script/console session to show our progress. I've narrowed the output down just to the interesting bits.