Skip to content

Commit

Permalink
Use klass.sti_name to make sure associations take store_full_sti_clas…
Browse files Browse the repository at this point in the history
…s into account. [#671 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
dguettler authored and tarmo committed Aug 24, 2008
1 parent c3aad22 commit c2f1918
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/associations.rb
Expand Up @@ -1875,7 +1875,7 @@ def association_join
jt_sti_extra = " AND %s.%s = %s" % [
connection.quote_table_name(aliased_join_table_name),
connection.quote_column_name(through_reflection.active_record.inheritance_column),
through_reflection.klass.quote_value(through_reflection.klass.name.demodulize)]
through_reflection.klass.quote_value(through_reflection.klass.sti_name)]
end
when :belongs_to
first_key = primary_key
Expand Down Expand Up @@ -1943,7 +1943,7 @@ def association_join
join << %(AND %s.%s = %s ) % [
connection.quote_table_name(aliased_table_name),
connection.quote_column_name(klass.inheritance_column),
klass.quote_value(klass.name.demodulize)] unless klass.descends_from_active_record?
klass.quote_value(klass.sti_name)] unless klass.descends_from_active_record?

[through_reflection, reflection].each do |ref|
join << "AND #{interpolate_sql(sanitize_sql(ref.options[:conditions]))} " if ref && ref.options[:conditions]
Expand Down
18 changes: 18 additions & 0 deletions activerecord/test/cases/associations/has_many_associations_test.rb
Expand Up @@ -995,4 +995,22 @@ def test_calling_first_or_last_with_integer_on_association_should_load_associati
assert firm.clients.loaded?
end

def test_joins_with_namespaced_model_should_use_correct_type
old = ActiveRecord::Base.store_full_sti_class
ActiveRecord::Base.store_full_sti_class = true

firm = Namespaced::Firm.create({ :name => 'Some Company' })
firm.clients.create({ :name => 'Some Client' })

stats = Namespaced::Firm.find(firm.id, {
:select => "#{Namespaced::Firm.table_name}.*, COUNT(#{Namespaced::Client.table_name}.id) AS num_clients",
:joins => :clients,
:group => "#{Namespaced::Firm.table_name}.id"
})
assert_equal 1, stats.num_clients.to_i

ensure
ActiveRecord::Base.store_full_sti_class = old
end

end
7 changes: 7 additions & 0 deletions activerecord/test/models/company.rb
Expand Up @@ -18,6 +18,13 @@ def arbitrary_method
module Namespaced
class Company < ::Company
end

class Firm < ::Company
has_many :clients, :class_name => 'Namespaced::Client'
end

class Client < ::Company
end
end

class Firm < Company
Expand Down

0 comments on commit c2f1918

Please sign in to comment.