diff options
author | Dana Sherson <robot@dana.sh> | 2018-04-20 10:56:49 +1000 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2018-04-20 17:28:25 -0400 |
commit | 75ef18c67c29b1b51314b6c8a963cee53394080b (patch) | |
tree | a4fb73cd8ebacae9fe1eeef4df70a3e1b265522b /activerecord/test/cases | |
parent | 7bdfc63cc245c14110e4ae092198137f01454129 (diff) | |
download | rails-75ef18c67c29b1b51314b6c8a963cee53394080b.tar.gz rails-75ef18c67c29b1b51314b6c8a963cee53394080b.tar.bz2 rails-75ef18c67c29b1b51314b6c8a963cee53394080b.zip |
Can preload associations through polymorphic associations
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r-- | activerecord/test/cases/associations/eager_test.rb | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 6e0cf30092..f46be8734b 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -1515,6 +1515,35 @@ class EagerAssociationTest < ActiveRecord::TestCase Author.preload(:readonly_comments).first! end + test "preloading through a polymorphic association doesn't require the association to exist" do + sponsors = [] + assert_queries 5 do + sponsors = Sponsor.where(sponsorable_id: 1).preload(sponsorable: [:post, :membership]).to_a + end + # check the preload worked + assert_queries 0 do + sponsors.map(&:sponsorable).map { |s| s.respond_to?(:posts) ? s.post.author : s.membership } + end + end + + test "preloading a regular association through a polymorphic association doesn't require the association to exist on all types" do + sponsors = [] + assert_queries 6 do + sponsors = Sponsor.where(sponsorable_id: 1).preload(sponsorable: [{ post: :first_comment }, :membership]).to_a + end + # check the preload worked + assert_queries 0 do + sponsors.map(&:sponsorable).map { |s| s.respond_to?(:posts) ? s.post.author : s.membership } + end + end + + test "preloading a regular association with a typo through a polymorphic association still raises" do + # this test contains an intentional typo of first -> fist + assert_raises(ActiveRecord::AssociationNotFoundError) do + Sponsor.where(sponsorable_id: 1).preload(sponsorable: [{ post: :fist_comment }, :membership]).to_a + end + end + private def find_all_ordered(klass, include = nil) klass.order("#{klass.table_name}.#{klass.primary_key}").includes(include).to_a |