November 20, 2005

validates_uniqueness_of

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.

Posted by dennis baldwin at November 20, 2005 10:02 PM
Comments