aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2014-06-04 07:55:59 -0300
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2014-06-04 07:55:59 -0300
commitc5c0bad190dff0495612dde2ac4e33f1ee1fa387 (patch)
tree7a5eb8075773679fc64b12ab32f7aa77ed5c9e97 /activerecord/test
parent2a6f4282b91d81dca2c58febc84ed98de9bea466 (diff)
parent2c555ec43ad254ab63cf1e572f999f7521c6cb04 (diff)
downloadrails-c5c0bad190dff0495612dde2ac4e33f1ee1fa387.tar.gz
rails-c5c0bad190dff0495612dde2ac4e33f1ee1fa387.tar.bz2
rails-c5c0bad190dff0495612dde2ac4e33f1ee1fa387.zip
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
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/associations/eager_test.rb4
-rw-r--r--activerecord/test/models/owner.rb12
2 files changed, 16 insertions, 0 deletions
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