diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2010-01-16 22:14:10 +0530 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2010-01-16 22:14:10 +0530 |
commit | cfdfd899262c79c37ac89e030f4d90c8f9868b50 (patch) | |
tree | f103f0d595bbbf40bc592cdd85db50fcb9a10bcf | |
parent | f80be3ea0fa2bb3416e180901d441b0834001c7f (diff) | |
download | rails-cfdfd899262c79c37ac89e030f4d90c8f9868b50.tar.gz rails-cfdfd899262c79c37ac89e030f4d90c8f9868b50.tar.bz2 rails-cfdfd899262c79c37ac89e030f4d90c8f9868b50.zip |
Use new finder methods for association preloading
-rw-r--r-- | activerecord/lib/active_record/association_preload.rb | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index 9f7b2a60b2..a43c4d09d6 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -188,11 +188,11 @@ module ActiveRecord conditions << append_conditions(reflection, preload_options) associated_records = reflection.klass.with_exclusive_scope do - reflection.klass.find(:all, :conditions => [conditions, ids], - :include => options[:include], - :joins => "INNER JOIN #{connection.quote_table_name options[:join_table]} t0 ON #{reflection.klass.quoted_table_name}.#{reflection.klass.primary_key} = t0.#{reflection.association_foreign_key}", - :select => "#{options[:select] || table_name+'.*'}, t0.#{reflection.primary_key_name} as the_parent_record_id", - :order => options[:order]) + reflection.klass.where([conditions, ids]). + includes(options[:include]). + joins("INNER JOIN #{connection.quote_table_name options[:join_table]} t0 ON #{reflection.klass.quoted_table_name}.#{reflection.klass.primary_key} = t0.#{reflection.association_foreign_key}"). + select("#{options[:select] || table_name+'.*'}, t0.#{reflection.primary_key_name} as the_parent_record_id"). + order(options[:order]).to_a end set_association_collection_records(id_to_record_map, reflection.name, associated_records, 'the_parent_record_id') end @@ -327,6 +327,7 @@ module ActiveRecord table_name = klass.quoted_table_name primary_key = klass.primary_key column_type = klass.columns.detect{|c| c.name == primary_key}.type + ids = id_map.keys.map do |id| if column_type == :integer id.to_i @@ -336,15 +337,14 @@ module ActiveRecord id end end + conditions = "#{table_name}.#{connection.quote_column_name(primary_key)} #{in_or_equals_for_ids(ids)}" conditions << append_conditions(reflection, preload_options) + associated_records = klass.with_exclusive_scope do - klass.find(:all, :conditions => [conditions, ids], - :include => options[:include], - :select => options[:select], - :joins => options[:joins], - :order => options[:order]) + klass.where([conditions, ids]).apply_finder_options(options.slice(:include, :select, :joins, :order)).to_a end + set_association_single_records(id_map, reflection.name, associated_records, primary_key) end end @@ -363,13 +363,12 @@ module ActiveRecord conditions << append_conditions(reflection, preload_options) reflection.klass.with_exclusive_scope do - reflection.klass.find(:all, - :select => (preload_options[:select] || options[:select] || "#{table_name}.*"), - :include => preload_options[:include] || options[:include], - :conditions => [conditions, ids], - :joins => options[:joins], - :group => preload_options[:group] || options[:group], - :order => preload_options[:order] || options[:order]) + reflection.klass.select(preload_options[:select] || options[:select] || "#{table_name}.*"). + includes(preload_options[:include] || options[:include]). + where([conditions, ids]). + joins(options[:joins]). + group(preload_options[:group] || options[:group]). + order(preload_options[:order] || options[:order]) end end |