Thursday, August 02, 2007

A Minor Rails Hiccup, With Solution

As mentioned, I've been working through Agile Web Development With Rails, 2E. I got to p. 88, which talks about adding data via a migration. So I download the data for 003_add_test_data.rb, and run rake on it. Nothing. All of my rows get deleted, but no new ones get added. I add some print lines, which get displayed when rake runs, so I know the code is getting executed. But no data gets added. Harumph!

Well, let's back up a bit. A few pages prior, I added validation to the model. Pretty cool stuff, being able to throw in some regular expressions and have a pretty error message show up on the page. So I did a little extra-curricular playing around, and life was good.

After looking around a bit, I discovered that the data that the book wants me to add wasn't passing my validation tests. Okay, fine. So why wasn't rake showing any of the validation exceptions?

Well, a quick post to the Rails forum later, it turns out that create() won't throw exceptions, it just adds entries into the 'errors' list and continues. create!() will throw an exception with the same error text that the Rails app shows, and stops the migration. Problem solved.

0 Comments:

Post a Comment

<< Home