diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-10-05 20:20:04 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-10-05 20:21:21 +0100 |
commit | 2e9e647fee59d975c9564d96c924d29ffe57f2a2 (patch) | |
tree | ed70277b1e139e88a63a05b967f36e9eb433d4b0 /activerecord/test | |
parent | 64747654ca661d695622c0ad9e33b8d9e6df8048 (diff) | |
download | rails-2e9e647fee59d975c9564d96c924d29ffe57f2a2.tar.gz rails-2e9e647fee59d975c9564d96c924d29ffe57f2a2.tar.bz2 rails-2e9e647fee59d975c9564d96c924d29ffe57f2a2.zip |
Raise an exception on unknown primary key inside AssociationReflection.
An association between two models cannot be made if a relevant key is
unknown, so fail fast rather than generating invalid SQL. Fixes #3207.
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/reflection_test.rb | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb index ca9d88fbd5..69e9fc8d61 100644 --- a/activerecord/test/cases/reflection_test.rb +++ b/activerecord/test/cases/reflection_test.rb @@ -18,6 +18,7 @@ require 'models/subscriber' require 'models/subscription' require 'models/tag' require 'models/sponsor' +require 'models/edge' class ReflectionTest < ActiveRecord::TestCase include ActiveRecord::Reflection @@ -252,11 +253,25 @@ class ReflectionTest < ActiveRecord::TestCase assert_equal "custom_primary_key", Author.reflect_on_association(:tags_with_primary_key).association_primary_key.to_s # nested end + def test_association_primary_key_raises_when_missing_primary_key + reflection = ActiveRecord::Reflection::AssociationReflection.new(:fuu, :edge, {}, Author) + assert_raises(ActiveRecord::UnknownPrimaryKey) { reflection.association_primary_key } + + through = ActiveRecord::Reflection::ThroughReflection.new(:fuu, :edge, {}, Author) + through.stubs(:source_reflection).returns(stub_everything(:options => {}, :class_name => 'Edge')) + assert_raises(ActiveRecord::UnknownPrimaryKey) { through.association_primary_key } + end + def test_active_record_primary_key assert_equal "nick", Subscriber.reflect_on_association(:subscriptions).active_record_primary_key.to_s assert_equal "name", Author.reflect_on_association(:essay).active_record_primary_key.to_s end + def test_active_record_primary_key_raises_when_missing_primary_key + reflection = ActiveRecord::Reflection::AssociationReflection.new(:fuu, :author, {}, Edge) + assert_raises(ActiveRecord::UnknownPrimaryKey) { reflection.active_record_primary_key } + end + def test_foreign_type assert_equal "sponsorable_type", Sponsor.reflect_on_association(:sponsorable).foreign_type.to_s assert_equal "sponsorable_type", Sponsor.reflect_on_association(:thing).foreign_type.to_s |