Hi! Thanks for visiting my blog. If you've received any value from my content would you mind supporting my new startup by downloading our browser add-on? It's called PriceBlink and makes online shopping a breeze. You can watch it in action here and download it for Chrome, Firefox, IE, or Safari by going to PriceBlink.com. Thank you and I hope you enjoy!

validates_uniqueness_of

Nov 20

I’ve spent a lot of time lately transitioning from ColdFusion to Ruby on Rails. I love CF, but I’ve been looking for a change of pace and RoR provides numerous features that make it extremely attractive. One of the reasons I started using CF was the fact that everything was simple and tag based. You could quickly crank out applications with minimal amounts of code. RoR has gone leaps and bounds towards minimizing the amount of code you have to write. I won’t go into details as there are numerous articles on the web about rails scaffolding.

A perfect example of minimal code is a task that I used to perform often in CF. I would check to see that a record exists in the database and then alert the user of its existence. This would be useful when making sure that a book title or username is unique. If you’ve done this in CF you’ve probably done the following:

[cfquery name="check"]
select count(*) as total from users where username=’#form.username#’;
[/cfquery]

[cfif check.total gt 0]
tell the user the username already exists
[/cfif]

In RoR this is as simple as placing the following statement in your model:

validates_uniqueness_of :username

and that’s it! RoR has some pretty incredible validation routines and also note that it’s more of a natural language syntax than CF. It will be interesting to see if any of the tag/scripting languages will inherit any of what RoR brings to the table.