From 715bfdea3576531e24af62d1968e76754f3707ca Mon Sep 17 00:00:00 2001 From: kane Date: Tue, 22 Jun 2010 17:12:38 -0700 Subject: quote scoped columns in validates_uniqueness_of [#4909 state:resolved] --- activerecord/lib/active_record/validations.rb | 2 +- activerecord/test/cases/validations_test.rb | 12 ++++++++++++ activerecord/test/schema/schema.rb | 1 + 3 files changed, 14 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 17cbcb9..ed1e7d2 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -825,7 +825,7 @@ module ActiveRecord if scope = configuration[:scope] Array(scope).map do |scope_item| scope_value = record.send(scope_item) - condition_sql << " AND " << attribute_condition("#{record.class.quoted_table_name}.#{scope_item}", scope_value) + condition_sql << " AND " << attribute_condition("#{record.class.quoted_table_name}.#{connection.quote_column_name(scope_item)}", scope_value) condition_params << scope_value end end diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index c33e4ea..6cea91e 100644 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -434,6 +434,18 @@ class ValidationsTest < ActiveRecord::TestCase end end + def test_validate_uniqueness_with_reserved_word_as_scope + repair_validations(Reply) do + Topic.validates_uniqueness_of(:content, :scope => "group") + + t1 = Topic.create "title" => "t1", "content" => "hello world2" + assert t1.valid? + + t2 = Topic.create "title" => "t2", "content" => "hello world2" + assert !t2.valid? + end + end + def test_validate_uniqueness_scoped_to_defining_class t = Topic.create("title" => "What, me worry?") diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index c8e652a..905b9e7 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -448,6 +448,7 @@ ActiveRecord::Schema.define do t.integer :parent_id t.string :parent_title t.string :type + t.string :group end create_table :taggings, :force => true do |t| -- 1.6.3.3