Forum: Ruby on Rails validates_uniqueness_of doesn't work if value tested is nume

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kiriakos Georgiou (Guest)
on 2007-07-30 20:01
(Received via mailing list)
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
Cynthia K. (Guest)
on 2007-07-31 04:41
(Received via mailing list)
> 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'
Matt B. (Guest)
on 2007-08-13 20:03
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
Jeremy B. (Guest)
on 2007-08-15 01:13
(Received via mailing list)
This is related to:

http://groups.google.com/group/rubyonrails-core/br...

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>
This topic is locked and can not be replied to.