Skip to content

Commit

Permalink
quote scoped columns in validates_uniqueness_of [#4909 state:resolved]
Browse files Browse the repository at this point in the history
Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
kane authored and josevalim committed Jun 23, 2010
1 parent 7d2173e commit 09a23d2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/validations.rb
Expand Up @@ -825,7 +825,7 @@ def validates_uniqueness_of(*attr_names)
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
Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/cases/validations_test.rb
Expand Up @@ -434,6 +434,18 @@ def test_validate_uniqueness_with_scope
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?")

Expand Down
1 change: 1 addition & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -460,6 +460,7 @@ def create_table(*args, &block)
t.integer :parent_id
t.string :parent_title
t.string :type
t.string :group
end

create_table :taggings, :force => true do |t|
Expand Down

0 comments on commit 09a23d2

Please sign in to comment.