Permalink
Browse files

allow association as 1st uniqueness validation arg

  • Loading branch information...
1 parent 5f47278 commit 3b5fbafab014325bdd42a7cae867ee5c92bc3298 Dan Pickett committed Jan 7, 2012
@@ -56,7 +56,15 @@ def find_finder_class_for(record) #:nodoc:
end
def build_relation(klass, table, attribute, value) #:nodoc:
- column = klass.columns_hash[attribute.to_s]
+ reflection = klass.reflect_on_association(attribute)
+ column = nil
+ if(reflection)
+ column = klass.columns_hash[reflection.foreign_key]
+ attribute = reflection.foreign_key
+ value = value.attributes[reflection.primary_key_column.name]
+ else
+ column = klass.columns_hash[attribute.to_s]
+ end
value = column.limit ? value.to_s[0, column.limit] : value.to_s if !value.nil? && column.text?
if !options[:case_sensitive] && value && column.text?
@@ -104,6 +104,18 @@ def test_validate_uniqueness_with_object_scope
assert !r2.valid?, "Saving r2 first time"
end
+ def test_validate_uniqueness_with_object_arg
+ Reply.validates_uniqueness_of(:topic)
+
+ t = Topic.create("title" => "I'm unique!")
+
+ r1 = t.replies.create "title" => "r1", "content" => "hello world"
+ assert r1.valid?, "Saving r1"
+
+ r2 = t.replies.create "title" => "r2", "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 3b5fbaf

Please sign in to comment.