public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Use klass.sti_name to make sure associations take store_full_sti_class into 
account. [#671 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
dguettler (author)
Mon Jul 21 12:21:13 -0700 2008
Tarmo Tänav (committer)
Sun Aug 24 09:26:57 -0700 2008
commit  c2f1918990b82516b1bf6142274e892df776ffff
tree    5ab8526642c92adb4c939d594f35819ebf3ab03b
parent  c3aad223321d1897c9e2269b32c3c2da7af814d1
...
1875
1876
1877
1878
 
1879
1880
1881
...
1943
1944
1945
1946
 
1947
1948
1949
...
1875
1876
1877
 
1878
1879
1880
1881
...
1943
1944
1945
 
1946
1947
1948
1949
0
@@ -1875,7 +1875,7 @@ module ActiveRecord
0
                           jt_sti_extra = " AND %s.%s = %s" % [
0
                             connection.quote_table_name(aliased_join_table_name),
0
                             connection.quote_column_name(through_reflection.active_record.inheritance_column),
0
-                            through_reflection.klass.quote_value(through_reflection.klass.name.demodulize)]
0
+                            through_reflection.klass.quote_value(through_reflection.klass.sti_name)]
0
                         end
0
                       when :belongs_to
0
                         first_key = primary_key
0
@@ -1943,7 +1943,7 @@ module ActiveRecord
0
               join << %(AND %s.%s = %s ) % [
0
                 connection.quote_table_name(aliased_table_name),
0
                 connection.quote_column_name(klass.inheritance_column),
0
-                klass.quote_value(klass.name.demodulize)] unless klass.descends_from_active_record?
0
+                klass.quote_value(klass.sti_name)] unless klass.descends_from_active_record?
0
 
0
               [through_reflection, reflection].each do |ref|
0
                 join << "AND #{interpolate_sql(sanitize_sql(ref.options[:conditions]))} " if ref && ref.options[:conditions]
...
995
996
997
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
998
...
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
0
@@ -995,4 +995,22 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
0
     assert firm.clients.loaded?
0
   end
0
 
0
+  def test_joins_with_namespaced_model_should_use_correct_type
0
+    old = ActiveRecord::Base.store_full_sti_class
0
+    ActiveRecord::Base.store_full_sti_class = true
0
+
0
+    firm = Namespaced::Firm.create({ :name => 'Some Company' })
0
+    firm.clients.create({ :name => 'Some Client' })
0
+
0
+    stats = Namespaced::Firm.find(firm.id, {
0
+      :select => "#{Namespaced::Firm.table_name}.*, COUNT(#{Namespaced::Client.table_name}.id) AS num_clients",
0
+      :joins  => :clients,
0
+      :group  => "#{Namespaced::Firm.table_name}.id"
0
+    })
0
+    assert_equal 1, stats.num_clients.to_i
0
+
0
+  ensure
0
+    ActiveRecord::Base.store_full_sti_class = old
0
+  end
0
+
0
 end
...
18
19
20
 
 
 
 
 
 
 
21
22
23
...
18
19
20
21
22
23
24
25
26
27
28
29
30
0
@@ -18,6 +18,13 @@ end
0
 module Namespaced
0
   class Company < ::Company
0
   end
0
+
0
+  class Firm < ::Company
0
+    has_many :clients, :class_name => 'Namespaced::Client'
0
+  end
0
+
0
+  class Client < ::Company
0
+  end
0
 end
0
 
0
 class Firm < Company

Comments