diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2010-12-25 16:19:59 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-12-25 16:19:59 -0700 |
commit | 5b918bb97cb1801945ef778508a3738da98012c5 (patch) | |
tree | ae9962ad745422c2d77c48a25bbcfb45bb357345 /activerecord/lib/active_record | |
parent | 0a609eea504f72baead7548d47f0fe707314a033 (diff) | |
download | rails-5b918bb97cb1801945ef778508a3738da98012c5.tar.gz rails-5b918bb97cb1801945ef778508a3738da98012c5.tar.bz2 rails-5b918bb97cb1801945ef778508a3738da98012c5.zip |
using arel to compile sql statements
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/association_preload.rb | 25 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/has_one_association.rb | 3 |
2 files changed, 19 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index 83383e9b19..7b4ff69b87 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -379,18 +379,20 @@ module ActiveRecord end def find_associated_records(ids, reflection, preload_options) - options = reflection.options + options = reflection.options + table = reflection.klass.arel_table table_name = reflection.klass.quoted_table_name + conditions = [] + + key = reflection.primary_key_name + if interface = reflection.options[:as] - conditions = "#{reflection.klass.quoted_table_name}.#{connection.quote_column_name "#{interface}_id"} #{in_or_equals_for_ids(ids)} and #{reflection.klass.quoted_table_name}.#{connection.quote_column_name "#{interface}_type"} = '#{self.base_class.sti_name}'" - else - foreign_key = reflection.primary_key_name - conditions = "#{reflection.klass.quoted_table_name}.#{foreign_key} #{in_or_equals_for_ids(ids)}" + key = "#{interface}_id" + conditions << table["#{interface}_type"].eq(base_class.sti_name) end - conditions = ([conditions] + - append_conditions(reflection, preload_options)).join(' AND ') + conditions += append_conditions(reflection, preload_options) find_options = { :select => preload_options[:select] || options[:select] || Arel.sql("#{table_name}.*"), @@ -401,7 +403,14 @@ module ActiveRecord } associated_records(ids) do |some_ids| - reflection.klass.scoped.apply_finder_options(find_options.merge(:conditions => [conditions, some_ids])).to_a + method = some_ids.length == 1 ? ['eq', some_ids.first] : + ['in', some_ids] + + where = conditions.inject(table[key].send(*method)) do |ast, cond| + ast.and cond + end + + reflection.klass.scoped.apply_finder_options(find_options.merge(:conditions => where)).to_a end end diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index c49fd6e66a..d08cbea199 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -89,7 +89,8 @@ module ActiveRecord "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_id = #{owner_quoted_id} AND " + "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}" else - sql = "#{@reflection.quoted_table_name}.#{@reflection.primary_key_name} = #{owner_quoted_id}" + test = owner_quoted_id == "NULL" ? "IS" : "=" + sql = "#{@reflection.quoted_table_name}.#{@reflection.primary_key_name} #{test} #{owner_quoted_id}" end sql << " AND (#{conditions})" if conditions { :conditions => sql } |