diff options
author | rsl <rsl@luckysneaks.com> | 2008-09-10 14:22:05 -0400 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-09-10 14:11:00 -0700 |
commit | 6ce13429cbc1359d85e1dc99c84561840e89d455 (patch) | |
tree | e9a489b72ef0cb3c84a182429847c44ba012c496 | |
parent | 5bbca48c227044a05e5c685249801d4230d7dd08 (diff) | |
download | rails-6ce13429cbc1359d85e1dc99c84561840e89d455.tar.gz rails-6ce13429cbc1359d85e1dc99c84561840e89d455.tar.bz2 rails-6ce13429cbc1359d85e1dc99c84561840e89d455.zip |
fixed association preloading to use = instead of IN when there's only one record
[#1013 state:committed]
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
-rw-r--r-- | activerecord/lib/active_record/association_preload.rb | 13 | ||||
-rw-r--r-- | activerecord/test/cases/inheritance_test.rb | 2 |
2 files changed, 9 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index 61fa34ac39..c60850fc77 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -95,7 +95,7 @@ module ActiveRecord records.each {|record| record.send(reflection.name).loaded} options = reflection.options - conditions = "t0.#{reflection.primary_key_name} IN (?)" + conditions = "t0.#{reflection.primary_key_name} #{in_or_equals_for_ids(ids)}" conditions << append_conditions(options, preload_options) associated_records = reflection.klass.find(:all, :conditions => [conditions, ids], @@ -222,8 +222,6 @@ module ActiveRecord table_name = klass.quoted_table_name primary_key = klass.primary_key - conditions = "#{table_name}.#{connection.quote_column_name(primary_key)} IN (?)" - conditions << append_conditions(options, preload_options) column_type = klass.columns.detect{|c| c.name == primary_key}.type ids = id_map.keys.uniq.map do |id| if column_type == :integer @@ -234,6 +232,8 @@ module ActiveRecord id end end + conditions = "#{table_name}.#{connection.quote_column_name(primary_key)} #{in_or_equals_for_ids(ids)}" + conditions << append_conditions(options, preload_options) associated_records = klass.find(:all, :conditions => [conditions, ids], :include => options[:include], :select => options[:select], @@ -248,10 +248,10 @@ module ActiveRecord table_name = reflection.klass.quoted_table_name if interface = reflection.options[:as] - conditions = "#{reflection.klass.quoted_table_name}.#{connection.quote_column_name "#{interface}_id"} IN (?) and #{reflection.klass.quoted_table_name}.#{connection.quote_column_name "#{interface}_type"} = '#{self.base_class.sti_name}'" + 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 (?)" + conditions = "#{reflection.klass.quoted_table_name}.#{foreign_key} #{in_or_equals_for_ids(ids)}" end conditions << append_conditions(options, preload_options) @@ -277,6 +277,9 @@ module ActiveRecord sql end + def in_or_equals_for_ids(ids) + ids.size > 1 ? "IN (?)" : "= ?" + end end end end diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb index 4fd38bfbc9..a39415618d 100644 --- a/activerecord/test/cases/inheritance_test.rb +++ b/activerecord/test/cases/inheritance_test.rb @@ -193,7 +193,7 @@ class InheritanceTest < ActiveRecord::TestCase def test_eager_load_belongs_to_primary_key_quoting con = Account.connection - assert_sql(/\(#{con.quote_table_name('companies')}.#{con.quote_column_name('id')} IN \(1\)\)/) do + assert_sql(/\(#{con.quote_table_name('companies')}.#{con.quote_column_name('id')} = 1\)/) do Account.find(1, :include => :firm) end end |