aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/base.rb
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2010-12-31 10:43:42 +0000
committerJon Leighton <j@jonathanleighton.com>2010-12-31 20:00:44 +0000
commit62b084f80759300f10a4e5c4235bf1d13693a7d3 (patch)
tree6eda5936f42ce5a14404edd8aa4823587b2bb3c4 /activerecord/lib/active_record/base.rb
parent2bf31868033c50d71d6d68c1ddad67147908adc4 (diff)
downloadrails-62b084f80759300f10a4e5c4235bf1d13693a7d3.tar.gz
rails-62b084f80759300f10a4e5c4235bf1d13693a7d3.tar.bz2
rails-62b084f80759300f10a4e5c4235bf1d13693a7d3.zip
Specify the STI type condition using SQL IN rather than a whole load of ORs. Required a fix to ActiveRecord::Relation#merge for properly merging create_with_value. This also fixes a situation where the type condition was appearing twice in the resultant SQL query.
Diffstat (limited to 'activerecord/lib/active_record/base.rb')
-rw-r--r--activerecord/lib/active_record/base.rb12
1 files changed, 8 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 396ab226a9..0a2e436ecc 100644
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -874,7 +874,12 @@ module ActiveRecord #:nodoc:
def relation #:nodoc:
@relation ||= Relation.new(self, arel_table)
- finder_needs_type_condition? ? @relation.where(type_condition) : @relation
+
+ if finder_needs_type_condition?
+ @relation.where(type_condition).create_with(inheritance_column.to_sym => sti_name)
+ else
+ @relation
+ end
end
# Finder methods must instantiate through this method to work with the
@@ -914,10 +919,9 @@ module ActiveRecord #:nodoc:
def type_condition
sti_column = arel_table[inheritance_column.to_sym]
- condition = sti_column.eq(sti_name)
- descendants.each { |subclass| condition = condition.or(sti_column.eq(subclass.sti_name)) }
+ sti_names = ([self] + descendants).map { |model| model.sti_name }
- condition
+ sti_column.in(sti_names)
end
# Guesses the table name, but does not decorate it with prefix and suffix information.