public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Fix integer quoting issues in association preload. [#602 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Tiago Macedo (author)
Thu Jul 10 20:18:41 -0700 2008
jeremy (committer)
Tue Jul 15 15:54:20 -0700 2008
commit  84baada079fa7e68750c1e386f9849d2d47b59e1
tree    74a8df621262eaa9b7791467c27d4a9f84408d5b
parent  97fa8547b2d3cf069a45fff76ecf5f0ce598fad1
...
188
189
190
191
192
193
194
...
227
228
229
230
 
231
232
 
 
 
 
 
 
 
 
 
 
 
233
234
235
...
188
189
190
 
191
192
193
...
226
227
228
 
229
230
 
231
232
233
234
235
236
237
238
239
240
241
242
243
244
0
@@ -188,7 +188,6 @@ module ActiveRecord
0
         through_records
0
       end
0
 
0
-      # FIXME: quoting
0
       def preload_belongs_to_association(records, reflection, preload_options={})
0
         options = reflection.options
0
         primary_key_name = reflection.primary_key_name
0
@@ -227,9 +226,19 @@ module ActiveRecord
0
 
0
           table_name = klass.quoted_table_name
0
           primary_key = klass.primary_key
0
-          conditions = "#{table_name}.#{primary_key} IN (?)"
0
+          conditions = "#{table_name}.#{connection.quote_column_name(primary_key)} IN (?)"
0
           conditions << append_conditions(options, preload_options)
0
-          associated_records = klass.find(:all, :conditions => [conditions, id_map.keys.uniq],
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
+              id.to_i
0
+            elsif column_type == :float
0
+              id.to_f
0
+            else
0
+              id
0
+            end
0
+          end
0
+          associated_records = klass.find(:all, :conditions => [conditions, ids],
0
                                           :include => options[:include],
0
                                           :select => options[:select],
0
                                           :joins => options[:joins],
...
191
192
193
 
 
 
 
 
 
 
194
195
196
...
191
192
193
194
195
196
197
198
199
200
201
202
203
0
@@ -191,6 +191,13 @@ class InheritanceTest < ActiveRecord::TestCase
0
     assert_not_nil account.instance_variable_get("@firm"), "nil proves eager load failed"
0
   end
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
+      Account.find(1, :include => :firm)
0
+    end
0
+  end
0
+
0
   def test_alt_eager_loading
0
     switch_to_alt_inheritance_column
0
     test_eager_load_belongs_to_something_inherited

Comments