Wednesday 20 October 2010

ActiveRecord validates_presence_of, case sensitivity and Rails 3 validations

The question arose this morning as to whether validates_presence_of in ActiveRecord was case sensitive by default or not. The documentation says it is case insensitive by default on 2.3 stream and case sensitive by default on 3.0 stream, but I dug down into ActiveRecord it looks to me like that's that isn't a change in behaviour, it's actually a fix in the documentation. It was also case sensitive by default in 2.3 stream.

activerecord-2.3.5/lib/active_record/validations.rb

774  def validates_uniqueness_of(*attr_names)
775    configuration = { :case_sensitive => true }

This explains some issues with exceptions being raised from MySql where we had a unique database index on a column, but we were validating with the default "validates_uniqueness_of".

One additional note on this. Making :case_sensitive => false changes the MySql query to

WHERE LOWER(table_name.column_name) = BINARY 'value'


Which will bypass the benefit of a database index on write. You'll still get the benefits of the index on read operations.

There is quite a lot of discussion around this issue in this lighthouse case. It's not a simple issue.

There are some patches to move around this issue for 2.3 involve detecting the database exceptions on index exceptions, foreign key exceptions and adding them into ActiveRecord errors. Which is a much nicer approach I think. Can see them here and here.

In Rails 3 the validations issue should be much simpler. The new validations code is so much more readable. And custom validation classes look really useful.

http://lindsaar.net/2010/1/31/validates_rails_3_awesome_is_true

Tuesday 19 October 2010

IE9 with CSS3 doesn't look promising

I suppose it's not entirely surprising but this comparison of CSS 3 on Safari, Chrome and IE9 Beta 4 really is a disappointment.

http://css3wizardry.com/2010/08/14/ie9-is-the-ie6-of-css3/