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
lifo (committer)
Mon Jul 14 15:34:19 -0700 2008
commit  76df9fa0680d62ce41fa6f3b743c605101d101d2
tree    c33756ffab05d20ac904fcdda2dc27955a4fac3e
parent  cd9b24286a90111a08002e0da753198c5fb2432a
...
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