diff --git a/activerecord/lib/active_record/associations/preloader.rb b/activerecord/lib/active_record/associations/preloader.rb index ecf6fb8643..e64af84e1a 100644 --- a/activerecord/lib/active_record/associations/preloader.rb +++ b/activerecord/lib/active_record/associations/preloader.rb @@ -184,6 +184,7 @@ module ActiveRecord def self.new(klass, owners, reflection, preload_scope); self; end def self.run(preloader); end def self.preloaded_records; []; end + def self.owners; []; end end # Returns a class containing the logic needed to load preload the data diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index bb8c9fa19c..aff0dabee7 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -11,7 +11,9 @@ require 'models/tagging' require 'models/author' require 'models/owner' require 'models/pet' +require 'models/pet_treasure' require 'models/toy' +require 'models/treasure' require 'models/contract' require 'models/company' require 'models/developer' @@ -1082,6 +1084,18 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert_equal [], person.posts end + def test_preloading_empty_through_with_polymorphic_source_association + owner = Owner.create!(name: "Rainbow Unicat") + pet = Pet.create!(owner: owner) + person = Person.create!(first_name: "Gaga") + treasure = Treasure.create!(looter: person) + non_looted_treasure = Treasure.create!() + PetTreasure.create!(pet: pet, treasure: treasure, rainbow_color: "Ultra violet indigo") + PetTreasure.create!(pet: pet, treasure: non_looted_treasure, rainbow_color: "Ultra violet indigo") + + assert_equal [person], Owner.where(name: "Rainbow Unicat").includes(pets: :persons).first.persons.to_a + end + def test_explicitly_joining_join_table assert_equal owners(:blackbeard).toys, owners(:blackbeard).toys.with_pet end diff --git a/activerecord/test/models/owner.rb b/activerecord/test/models/owner.rb index cedb774b10..ce8242cf2f 100644 --- a/activerecord/test/models/owner.rb +++ b/activerecord/test/models/owner.rb @@ -1,7 +1,8 @@ class Owner < ActiveRecord::Base self.primary_key = :owner_id has_many :pets, -> { order 'pets.name desc' } - has_many :toys, :through => :pets + has_many :toys, through: :pets + has_many :persons, through: :pets belongs_to :last_pet, class_name: 'Pet' scope :including_last_pet, -> { diff --git a/activerecord/test/models/pet.rb b/activerecord/test/models/pet.rb index f7970d7aab..53489fa1b3 100644 --- a/activerecord/test/models/pet.rb +++ b/activerecord/test/models/pet.rb @@ -4,6 +4,9 @@ class Pet < ActiveRecord::Base self.primary_key = :pet_id belongs_to :owner, :touch => true has_many :toys + has_many :pet_treasures + has_many :treasures, through: :pet_treasures + has_many :persons, through: :treasures, source: :looter, source_type: 'Person' class << self attr_accessor :after_destroy_output diff --git a/activerecord/test/models/pet_treasure.rb b/activerecord/test/models/pet_treasure.rb new file mode 100644 index 0000000000..1fe7807ffe --- /dev/null +++ b/activerecord/test/models/pet_treasure.rb @@ -0,0 +1,6 @@ +class PetTreasure < ActiveRecord::Base + self.table_name = "pets_treasures" + + belongs_to :pet + belongs_to :treasure +end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 2bcdc8729e..1027bcb365 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -614,6 +614,12 @@ ActiveRecord::Schema.define do end end + create_table :pets_treasures, force: true do |t| + t.column :treasure_id, :integer + t.column :pet_id, :integer + t.column :rainbow_color, :string + end + create_table :pirates, force: true do |t| t.column :catchphrase, :string t.column :parrot_id, :integer