diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2007-10-08 01:44:55 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2007-10-08 01:44:55 +0000 |
commit | 210ecaecc3d5019adf9fde496ff0a1015ec23110 (patch) | |
tree | 3c87aa59201d34cf871f8b3c2ef10e17bb53f2ab /activerecord/test | |
parent | e7b80cbbbc4279b9408660b9bbb1ecb9807a8497 (diff) | |
download | rails-210ecaecc3d5019adf9fde496ff0a1015ec23110.tar.gz rails-210ecaecc3d5019adf9fde496ff0a1015ec23110.tar.bz2 rails-210ecaecc3d5019adf9fde496ff0a1015ec23110.zip |
validates_uniqueness_of behaves well with single-table inheritance. Closes #3833.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7787 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rwxr-xr-x | activerecord/test/validations_test.rb | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb index 4b91d86b18..49fc30eff7 100755 --- a/activerecord/test/validations_test.rb +++ b/activerecord/test/validations_test.rb @@ -21,6 +21,18 @@ class ProtectedPerson < ActiveRecord::Base attr_protected :first_name end +class UniqueReply < Reply + validates_uniqueness_of :content, :scope => 'parent_id' +end + +class SillyUniqueReply < UniqueReply +end + +class Topic < ActiveRecord::Base + has_many :unique_replies, :dependent => :destroy, :foreign_key => "parent_id" + has_many :silly_unique_replies, :dependent => :destroy, :foreign_key => "parent_id" +end + class ValidationsTest < Test::Unit::TestCase fixtures :topics, :developers @@ -320,6 +332,21 @@ class ValidationsTest < Test::Unit::TestCase assert r3.valid?, "Saving r3" end + def test_validate_uniqueness_scoped_to_defining_class + t = Topic.create("title" => "What, me worry?") + + r1 = t.unique_replies.create "title" => "r1", "content" => "a barrel of fun" + assert r1.valid?, "Saving r1" + + r2 = t.silly_unique_replies.create "title" => "r2", "content" => "a barrel of fun" + assert !r2.valid?, "Saving r2" + + # Should succeed as validates_uniqueness_of only applies to + # UniqueReply and it's subclasses + r3 = t.replies.create "title" => "r2", "content" => "a barrel of fun" + assert r3.valid?, "Saving r3" + end + def test_validate_uniqueness_with_scope_array Reply.validates_uniqueness_of(:author_name, :scope => [:author_email_address, :parent_id]) |