# Validates\_uniqueness\_of doesn't work if value tested is nume

**URL:** <https://www.ruby-forum.com/t/validates-uniqueness-of-doesnt-work-if-value-tested-is-nume/110758>\
**Category:** Rails\
**Created:** [July 30, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-doesnt-work-if-value-tested-is-nume/110758 "2007-07-30T18:01:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kiriakos\_Georgiou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@Kiriakos\_Georgiou](https://www.ruby-forum.com/u/Kiriakos_Georgiou)\
**Post date:** [July 30, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-doesnt-work-if-value-tested-is-nume/110758/1 "2007-07-30T18:01:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kiriakos\_Georgiou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@Kiriakos\_Georgiou](https://www.ruby-forum.com/u/Kiriakos_Georgiou)\
**Post date:** [July 31, 2007, 2:41am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-doesnt-work-if-value-tested-is-nume/110758/2 "2007-07-31T02:41:16Z")

</div>

> 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’

---

<div class="post-metadata">

**Author:** ![Kiriakos\_Georgiou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@Kiriakos\_Georgiou](https://www.ruby-forum.com/u/Kiriakos_Georgiou)\
**Post date:** [August 13, 2007, 6:03pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-doesnt-work-if-value-tested-is-nume/110758/3 "2007-08-13T18:03:46Z")

</div>

Cynthia K. wrote:

> > 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’

I am having this similar behavior, to validate the uniqueness of a  
title, but when passing in a number it fails, b/c the parameter is not  
being seen as a String. Here is the validator -

validates\_uniqueness\_of :title,  
:message =\>  
ApplicationConstants::REPORT\_SPEC\_TITLE\_TAKEN\_MSG,  
:case\_sensitive =\> false,  
:scope =\> [:associateID, :view\_type\_id],  
:if =\> Proc.new {|spec| !spec.is\_a?(ReportSpecificationClone)  
}

We are using the sybase adapter as well.

Tried this in the rails console -  
Create a spec  
set title = 123.to\_s  
and get the same results that web app gets.

ActiveRecord::StatementInvalid: RuntimeError: SQL Command Failed for  
ReportSpecification Load: SELECT \* FROM report\_specifications WHERE  
(LOWER(report\_specifications.title) = 123 AND  
report\_specifications.associateID = 2500000 AND  
report\_specifications.view\_type\_id = 1 AND report\_specifications.id \<\>  
471)  
Message: Implicit conversion from datatype ‘VARCHAR’ to ‘INT’ is not  
allowed. Use the CONVERT function to run this query.  
: SELECT \* FROM report\_specifications WHERE  
(LOWER(report\_specifications.title) = 123 AND  
report\_specifications.associateID = 2500000 AND  
report\_specifications.view\_type\_id = 1 AND report\_specifications.id \<\>  
471)

Any help or insight to the problem is appreciated.

Thanks

---

<div class="post-metadata">

**Author:** ![Kiriakos\_Georgiou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@Kiriakos\_Georgiou](https://www.ruby-forum.com/u/Kiriakos_Georgiou)\
**Post date:** [August 14, 2007, 11:13pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-doesnt-work-if-value-tested-is-nume/110758/4 "2007-08-14T23:13:11Z")

</div>

This is related to:

[http://groups.google.com/group/rubyonrails-core/browse\_thread/thread/572706bfc0ebd894/524df0e3fbcf395f?lnk=gst&q=numconvert&rnum=1#524df0e3fbcf395f](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.

On Aug 13, 11:03 am, Matt B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
