Showing posts with label tests. Show all posts
Showing posts with label tests. Show all posts

Wednesday, June 22, 2011

Rails Validation Syntax

Tonight while working through Agile Web Development with Rails, I was finishing up Chapter 7 and doing some of the extra stuff at the end. One of the exercises was adding validation to the Product model that checks the length of product titles is at least 10 characters.

No big deal! I fired up Rails API and found what I was looking for.

validates_length_of :title, :minimum => 10, :message => 'must be at least 10 characters.'


Ahh, perfect! Plus, editing the default message is the second optional exercise. Easy enough. But wait, what's this?

The book had me write validation like so:

validates :price, :numericality => {:greater_than_or_equal_to => 0.01}, :message => 'must be a number.'


This is a slightly different syntax than the validation I found on the Rails API and I don't like it as much. First, it's not as immediately obvious what this validation is doing. Second, it just feels clunkier. So since I'd already written a validation method in a different syntax, I figured this would do the same thing and be in a more readable format:

validates_numericality_of :price, :greater_than_or_equal_to => 0.01, :message => 'must be a number.'


And what do you know? It works! Is there anything I should be aware of when writing validation like this? Any ideas why Agile Web Development with Rails wrote it the other way? Personal preference of the programmer, perhaps? I'm assuming this is a standard way of writing validation, both because it's the same syntax I found on the Rails API, and because it's more readable. (It's also 5 characters shorter, but who's counting? A piece of Javascript I found online, that's who!)

Ahh, feels good to be making progress. I got hung up for a bit writing tests in the last chapter, until I realized I'd added a stray period and it was holding everything up. Anyone else try running test:unites by mistake? I've probably done it 3 or 5 times in the last 18 hours.

Monday, April 18, 2011

Headaches and fixing mistakes

You want to talk about Rails headaches? Ok, here we go.

While working on this shopping cart application this evening, I accidentally entered some code into the carts_controller test that was supposed to go into the line_items test. At first I just got frustrated, then tried deleting the cart scaffold and then generating a new cart scaffold. Unfortunately, the carts table already existed, so this didn't work. Then I tried creating a new scaffold called shopping_cart, but I realized that would be too tedious to work with, since every instance in AWD4R will mention cart instead of shopping_cart. Finally I just rolled back the migration, then generated the scaffold again and that worked. Success!

What did I learn from this?
Pay attention.
Keep calm and carry on.
Don't try to hack my way around problems - try solving them correctly.

I'm proud of myself for not quitting when I realized I had a problem and for eventually finding the correct solution. Other than this little hiccup (and it WAS a little hiccup, all things considered) I'm making a ton of progress. What does it say about me that I can't wait to get back to work on Manticore, now that I know how to run rake tests and fix errors that it finds?

One last thing. Speaking as someone with absolutely zero development experience, when I decided to take the advice of my friend Eli (aka jqr) I faced a gigantic learning curve. Here's how I got over my fear of just how much I had to learn: I looked the learning curve right in the eyes and said FUCK YOU. The best way I've found to deal with feeling overwhelmed or in over my head is to find a way to stop being scared of something new and start getting excited about it. Rails is no different.