Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #15482 from laurocaetano/fix-regression-for-eager-…

…load

Fix regression on eager loading association based on SQL query rather than existing column.

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/test/cases/associations/eager_test.rb
  • Loading branch information...
commit 10904667d5a9445131ecd18528d1f277b15bf587 1 parent 3d2f340
@carlosantoniodasilva carlosantoniodasilva authored
View
7 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*
+
* Fixed `columns_for_distinct` of postgresql adapter to work correctly
with orders without sort direction modifiers.
View
6 activerecord/lib/active_record/associations/preloader/association.rb
@@ -104,11 +104,13 @@ def key_conversion_required?
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)
View
4 activerecord/test/cases/associations/eager_test.rb
@@ -1248,4 +1248,8 @@ def test_deep_including_through_habtm
assert_equal 2, author.posts.size
}
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
end
View
12 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

0 comments on commit 1090466

Please sign in to comment.
Something went wrong with that request. Please try again.