Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11366 (closed defect: fixed)

Opened 6 months ago

Last modified 5 months ago

[PATCH] Case sensitive validate_uniqueness_of validation

Reported by: miloops Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords: validation
Cc:

Description

validate_uniqueness_of has the option case_sensite (and is the default) at the moment, but isn't working, because queries aren't case sensitive.

As there isn't a possible SQL query fix, the current implementation is invalid. I attach a patch with a solution and test that show it is working now.

The solution is simple, if the find finds something, it does a ruby (case sensitive) comparison if the option was set to to true.

Also fixed a documentation error.

Attachments

fix_case_sensitive_validation.diff (6.3 kB) - added by miloops on 03/22/08 23:34:57.

Change History

03/17/08 17:43:49 changed by lifofifo

Your method is valid when you have just 2 records. But it'll fail when you have case sensitivity on and have more than 2 records, as you cannot ensure the order in which the results are returned.

Of course you could do find(:all) and go through all the returned results. But I'd be more interested in solving this with database, rather than ruby ( if possible using a db agnostic way ).

I'm not sure about other databases, but for mysql, you could do :

select * from users where login LIKE BINARY 'guest'

Also, check http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html

03/17/08 19:25:02 changed by miloops

lifofifo you were right, i did the patch again adding test to check more than 2 records, i also did some research about doing this from SQL before opening this ticket but there isn't an database agnostic way of solving case sensitivity i think.

I did the patch again using find(:all) but only when :case_sensitive is set to true, otherwise it will use find(:first). I think this is the best way possible to do this and if the option is available it should be working.

03/22/08 23:34:57 changed by miloops

  • attachment fix_case_sensitive_validation.diff added.

03/31/08 01:50:10 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.

(In [9160]) Fix case-sensitive validates_uniqueness_of. Closes #11366 [miloops]