public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
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>
rsl (author)
Wed Sep 10 11:22:05 -0700 2008
jeremy (committer)
Wed Sep 10 14:11:00 -0700 2008
commit  6ce13429cbc1359d85e1dc99c84561840e89d455
tree    e9a489b72ef0cb3c84a182429847c44ba012c496
parent  5bbca48c227044a05e5c685249801d4230d7dd08
...
95
96
97
98
 
99
100
101
...
222
223
224
225
226
227
228
229
...
234
235
236
 
 
237
238
239
...
248
249
250
251
 
252
253
254
 
255
256
257
...
277
278
279
 
 
 
280
281
282
...
95
96
97
 
98
99
100
101
...
222
223
224
 
 
225
226
227
...
232
233
234
235
236
237
238
239
...
248
249
250
 
251
252
253
 
254
255
256
257
...
277
278
279
280
281
282
283
284
285
0
@@ -95,7 +95,7 @@ module ActiveRecord
0
         records.each {|record| record.send(reflection.name).loaded}
0
         options = reflection.options
0
 
0
-        conditions = "t0.#{reflection.primary_key_name}  IN (?)"
0
+        conditions = "t0.#{reflection.primary_key_name} #{in_or_equals_for_ids(ids)}"
0
         conditions << append_conditions(options, preload_options)
0
 
0
         associated_records = reflection.klass.find(:all, :conditions => [conditions, ids],
0
@@ -222,8 +222,6 @@ module ActiveRecord
0
 
0
           table_name = klass.quoted_table_name
0
           primary_key = klass.primary_key
0
-          conditions = "#{table_name}.#{connection.quote_column_name(primary_key)} IN (?)"
0
-          conditions << append_conditions(options, preload_options)
0
           column_type = klass.columns.detect{|c| c.name == primary_key}.type
0
           ids = id_map.keys.uniq.map do |id|
0
             if column_type == :integer
0
@@ -234,6 +232,8 @@ module ActiveRecord
0
               id
0
             end
0
           end
0
+          conditions = "#{table_name}.#{connection.quote_column_name(primary_key)} #{in_or_equals_for_ids(ids)}"
0
+          conditions << append_conditions(options, preload_options)
0
           associated_records = klass.find(:all, :conditions => [conditions, ids],
0
                                           :include => options[:include],
0
                                           :select => options[:select],
0
@@ -248,10 +248,10 @@ module ActiveRecord
0
         table_name = reflection.klass.quoted_table_name
0
 
0
         if interface = reflection.options[:as]
0
-          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}'"
0
+          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}'"
0
         else
0
           foreign_key = reflection.primary_key_name
0
-          conditions = "#{reflection.klass.quoted_table_name}.#{foreign_key} IN (?)"
0
+          conditions = "#{reflection.klass.quoted_table_name}.#{foreign_key} #{in_or_equals_for_ids(ids)}"
0
         end
0
 
0
         conditions << append_conditions(options, preload_options)
0
@@ -277,6 +277,9 @@ module ActiveRecord
0
         sql
0
       end
0
 
0
+      def in_or_equals_for_ids(ids)
0
+        ids.size > 1 ? "IN (?)" : "= ?"
0
+      end
0
     end
0
   end
0
 end
...
193
194
195
196
 
197
198
199
...
193
194
195
 
196
197
198
199
0
@@ -193,7 +193,7 @@ class InheritanceTest < ActiveRecord::TestCase
0
 
0
   def test_eager_load_belongs_to_primary_key_quoting
0
     con = Account.connection
0
-    assert_sql(/\(#{con.quote_table_name('companies')}.#{con.quote_column_name('id')} IN \(1\)\)/) do
0
+    assert_sql(/\(#{con.quote_table_name('companies')}.#{con.quote_column_name('id')} = 1\)/) do
0
       Account.find(1, :include => :firm)
0
     end
0
   end

Comments