Skip to content

Commit

Permalink
Fix regression on eager loading association based on SQL query rather
Browse files Browse the repository at this point in the history
than existing column.

Fixes #15480.
  • Loading branch information
laurocaetano committed Jun 3, 2014
1 parent 4bcf902 commit 2c555ec
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
7 changes: 7 additions & 0 deletions 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 * Preserve type when dumping PostgreSQL point, bit, bit varying and money
columns. columns.


Expand Down
Expand Up @@ -104,11 +104,13 @@ def key_conversion_required?
end end


def association_key_type 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 end


def owner_key_type 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 end


def load_slices(slices) def load_slices(slices)
Expand Down
4 changes: 4 additions & 0 deletions activerecord/test/cases/associations/eager_test.rb
Expand Up @@ -1239,6 +1239,10 @@ def test_deep_including_through_habtm
} }
end 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 test "include instance dependent associations is deprecated" do
message = "association scope 'posts_with_signature' is" message = "association scope 'posts_with_signature' is"
assert_deprecated message do assert_deprecated message do
Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/models/owner.rb
Expand Up @@ -3,6 +3,18 @@ class Owner < ActiveRecord::Base
has_many :pets, -> { order 'pets.name desc' } has_many :pets, -> { order 'pets.name desc' }
has_many :toys, :through => :pets 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 after_commit :execute_blocks


def blocks def blocks
Expand Down

0 comments on commit 2c555ec

Please sign in to comment.