From cf1904f65bffcf6833903d4a30770eddc5713486 Mon Sep 17 00:00:00 2001 From: Ben Maraney Date: Fri, 30 Aug 2013 12:25:10 +0300 Subject: Fix STI scopes using benolee's suggestion. Fixes #11939 --- activerecord/lib/active_record/relation/spawn_methods.rb | 2 +- activerecord/test/cases/inheritance_test.rb | 5 +++++ activerecord/test/models/company.rb | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb index b734fc5c51..93fa30eb38 100644 --- a/activerecord/lib/active_record/relation/spawn_methods.rb +++ b/activerecord/lib/active_record/relation/spawn_methods.rb @@ -151,7 +151,7 @@ module ActiveRecord values = other.joins_values return if values.blank? - if other.klass == relation.klass + if other.klass >= relation.klass relation.joins_values += values else joins_dependency, rest = values.partition do |join| diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb index 99a252a389..e4cd89e563 100644 --- a/activerecord/test/cases/inheritance_test.rb +++ b/activerecord/test/cases/inheritance_test.rb @@ -236,6 +236,11 @@ class InheritanceTest < ActiveRecord::TestCase assert_nothing_raised { s = SpecialSubscriber.new("name" => "And breaaaaathe!"); s.id = 'roger'; s.save } end + def test_scope_inherited_properly + assert_nothing_raised { Company.of_first_firm } + assert_nothing_raised { Client.of_first_firm } + end + private def switch_to_alt_inheritance_column # we don't want misleading test results, so get rid of the values in the type column diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index f8e259fdfe..3440c75b68 100644 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -12,6 +12,11 @@ class Company < AbstractCompany has_many :contracts has_many :developers, :through => :contracts + scope :of_first_firm, lambda { + joins(:account => :firm). + where('firms.id' => 1) + } + def arbitrary_method "I am Jack's profound disappointment" end -- cgit v1.2.3