validates_uniqueness_of doesn't work if value tested is numeric and column is string

Hello all,

I am working through the depot example in the rails book. If I add a validates_uniqueness_of to a string column in the model, and then try to create a record with a number in this string column I get an SQL error. Looks like the SQL generated to test for uniqueness does not quote the value if it's numeric even though it's comparing against a varchar column. It does something like this: (select * from products where title = 123) This of course needs 123 to be quoted in order for it to work.

I thought this would be an easy fix in the quote() method of sybase_adapter.rb but it appears to be doing the right thing by checking the column type.

I'd like to figure out if it's specific to the sybase adapter so if anyone can validates_uniqueness_of a string column and then pass an integer I'd appreciate it. Also that's the proper way of debugging sybase_adapter.rb's quote() method? At a minimum I'd like to pepper it with some print statements and be able to see the output in one of the log files.

thanks, Kiriakos ps. Few days into RoR, all I can say is wow

I am working through the depot example in the rails book. If I add a validates_uniqueness_of to a string column in the model, and then try to create a record with a number in this string column I get an SQL error. Looks like the SQL generated to test for uniqueness does not quote the value if it's numeric even though it's comparing against a varchar column. It does something like this: (select * from products where title = 123) This of course needs 123 to be quoted in order for it to work.

I think you need to cast 123 to a string. You didn't give enough context, but before you do whatever was generating the select, try updating the title parameter to be 'title.to_s'

This is related to:

http://groups.google.com/group/rubyonrails-core/browse_thread/thread/572706bfc0ebd894/524df0e3fbcf395f?lnk=gst&q=numconvert&rnum=1#524df0e3fbcf395f

validates_uniqueness_of ends up calling #quote_bound_value. as a result no column information is passed into #quote. As a result the value is never quoted.