Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixes "Cannot visit ..." with validates_uniqueness_of

Fixes issue with overrding ActiveRecord reader methods with a
composed object and using that attribute as the scope of a
validates_uniqueness_of validation.
  • Loading branch information...
commit 0c3c2279e7f9b92f606356debc2efe0a93752a69 1 parent 15bfaa3
@beerlington beerlington authored
View
2  activerecord/lib/active_record/validations/uniqueness.rb
@@ -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(options[:scope]).each do |scope_item|
- scope_value = record.send(scope_item)
+ scope_value = record.read_attribute(scope_item)
reflection = record.class.reflect_on_association(scope_item)
if reflection
scope_value = record.send(reflection.foreign_key)
View
16 activerecord/test/cases/validations/uniqueness_validation_test.rb
@@ -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
@@ -104,6 +112,14 @@ def test_validate_uniqueness_with_object_scope
assert !r2.valid?, "Saving r2 first time"
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_with_object_arg
Reply.validates_uniqueness_of(:topic)
Please sign in to comment.
Something went wrong with that request. Please try again.