diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2010-12-22 17:30:57 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-12-22 18:23:37 -0800 |
commit | 186a1b11449a2b94030791b2c778a76b07bb099f (patch) | |
tree | 84514e6dd6eeec118be6a3c94887c676de5c06df /activerecord/lib/active_record | |
parent | 5853583f9b810d7186ad36801b3a97ed49f77799 (diff) | |
download | rails-186a1b11449a2b94030791b2c778a76b07bb099f.tar.gz rails-186a1b11449a2b94030791b2c778a76b07bb099f.tar.bz2 rails-186a1b11449a2b94030791b2c778a76b07bb099f.zip |
build an AST rather than build SQL strings
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/association_preload.rb | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index f2094283a2..9504b00515 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -185,6 +185,9 @@ module ActiveRecord end def preload_has_and_belongs_to_many_association(records, reflection, preload_options={}) + + left = reflection.klass.arel_table + table_name = reflection.klass.quoted_table_name id_to_record_map, ids = construct_id_map(records) records.each {|record| record.send(reflection.name).loaded} @@ -193,9 +196,15 @@ module ActiveRecord conditions = "t0.#{reflection.primary_key_name} #{in_or_equals_for_ids(ids)}" conditions << append_conditions(reflection, preload_options) + right = Arel::Table.new(options[:join_table]).alias('t0') + condition = left[reflection.klass.primary_key].eq( + right[reflection.association_foreign_key]) + + join = left.create_join(right, left.create_on(condition)) + associated_records_proxy = reflection.klass.unscoped. 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}"). + joins(join). select("#{options[:select] || table_name+'.*'}, t0.#{reflection.primary_key_name} as the_parent_record_id"). order(options[:order]) |