Thursday, December 10, 2009

validates_uniqueness_of issue resolved

Recently I had an issue when Rails validation validates_uniqueness_of did not work on one of my table columns. When I looked at the Rails logs, the query to check if the value supplied in form exists was using empty string in the where clause. Then I looked at the validation code in ActiveRecord and it turned out that the column had type of text in the database and this validation skips columns of type text. Actually I never meant this column to be type text and even the migration showed it of type string (not sure why this happened) which should have mapped this column to varchar. But once I changed the type of column to varchar(255), everything worked fine. I was using JRuby, Rails 2.3.2 and Sqlite3 database on windows XP. Hope it helps somebody else.