diff options
author | Lauro Caetano <laurocaetano1@gmail.com> | 2014-06-02 23:07:02 -0300 |
---|---|---|
committer | Lauro Caetano <laurocaetano1@gmail.com> | 2014-06-03 16:20:08 -0300 |
commit | 2c555ec43ad254ab63cf1e572f999f7521c6cb04 (patch) | |
tree | a4bb9b3cd4364005ec0f802baae1b93273fc5f64 /activerecord | |
parent | 4bcf9029452e0c760af04faab6b549710401e8cf (diff) | |
download | rails-2c555ec43ad254ab63cf1e572f999f7521c6cb04.tar.gz rails-2c555ec43ad254ab63cf1e572f999f7521c6cb04.tar.bz2 rails-2c555ec43ad254ab63cf1e572f999f7521c6cb04.zip |
Fix regression on eager loading association based on SQL query rather
than existing column.
Fixes #15480.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 7 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/preloader/association.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/associations/eager_test.rb | 4 | ||||
-rw-r--r-- | activerecord/test/models/owner.rb | 12 |
4 files changed, 27 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 02fa504c78..90ceb40406 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,10 @@ +* Fix regression on eager loading association based on SQL query rather than + existing column. + + Fixes #15480. + + *Lauro Caetano*, *Carlos Antonio da Silva* + * Preserve type when dumping PostgreSQL point, bit, bit varying and money columns. diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb index 63773bd5e1..1b83700613 100644 --- a/activerecord/lib/active_record/associations/preloader/association.rb +++ b/activerecord/lib/active_record/associations/preloader/association.rb @@ -104,11 +104,13 @@ module ActiveRecord end def association_key_type - @klass.column_types[association_key_name.to_s].type + column = @klass.column_types[association_key_name.to_s] + column && column.type end def owner_key_type - @model.column_types[owner_key_name.to_s].type + column = @model.column_types[owner_key_name.to_s] + column && column.type end def load_slices(slices) diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 4bd4486b41..910067666a 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -1239,6 +1239,10 @@ class EagerAssociationTest < ActiveRecord::TestCase } end + test "including association based on sql condition and no database column" do + assert_equal pets(:parrot), Owner.including_last_pet.first.last_pet + end + test "include instance dependent associations is deprecated" do message = "association scope 'posts_with_signature' is" assert_deprecated message do diff --git a/activerecord/test/models/owner.rb b/activerecord/test/models/owner.rb index cf24502d3a..2e3a9a3681 100644 --- a/activerecord/test/models/owner.rb +++ b/activerecord/test/models/owner.rb @@ -3,6 +3,18 @@ class Owner < ActiveRecord::Base has_many :pets, -> { order 'pets.name desc' } has_many :toys, :through => :pets + belongs_to :last_pet, class_name: 'Pet' + scope :including_last_pet, -> { + select(%q[ + owners.*, ( + select p.pet_id from pets p + where p.owner_id = owners.owner_id + order by p.name desc + limit 1 + ) as last_pet_id + ]).includes(:last_pet) + } + after_commit :execute_blocks def blocks |