Permalink
Browse files

Backported #7072 to 3-2-stable. Use database value for uniqueness val…

…idation scope.
  • Loading branch information...
1 parent 0761bb0 commit 905b7df1fac8332547b9eb7fc4d5b083c6fc1031 @authorNari authorNari committed Feb 26, 2013
@@ -1,4 +1,10 @@
## unreleased ##
+* Fixes issue with overrding ActiveRecord reader methods with a
+ composed object and using that attribute as the scope of a
+ validates_uniqueness_of validation.
+ Backport of #7072
+
+ *Peter Brown*
* Sqlite now preserves custom primary keys when copying or altering tables.
Fixes #9367.
@@ -26,7 +26,7 @@ def validate_each(record, attribute, value)
relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.send(:id))) if record.persisted?
Array.wrap(options[:scope]).each do |scope_item|
- scope_value = record.send(scope_item)
+ scope_value = record.read_attribute(scope_item)
relation = relation.and(table[scope_item].eq(scope_value))
end
@@ -22,6 +22,14 @@ class Conjurer < IneptWizard
class Thaumaturgist < IneptWizard
end
+class ReplyTitle; end
+
+class ReplyWithTitleObject < Reply
+ validates_uniqueness_of :content, :scope => :title
+
+ def title; ReplyTitle.new; end
+end
+
class UniquenessValidationTest < ActiveRecord::TestCase
fixtures :topics, 'warehouse-things', :developers
@@ -80,6 +88,14 @@ def test_validate_uniqueness_with_scope
assert r3.valid?, "Saving r3"
end
+ def test_validate_uniqueness_with_composed_attribute_scope
+ r1 = ReplyWithTitleObject.create "title" => "r1", "content" => "hello world"
+ assert r1.valid?, "Saving r1"
+
+ r2 = ReplyWithTitleObject.create "title" => "r1", "content" => "hello world"
+ assert !r2.valid?, "Saving r2 first time"
+ end
+
def test_validate_uniqueness_scoped_to_defining_class
t = Topic.create("title" => "What, me worry?")

0 comments on commit 905b7df

Please sign in to comment.