Skip to content
This repository
Browse code

Fix integer quoting issues in association preload. [#602 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
commit 76df9fa0680d62ce41fa6f3b743c605101d101d2 1 parent cd9b242
Tiago Macedo authored lifo committed
15 activerecord/lib/active_record/association_preload.rb
@@ -188,7 +188,6 @@ def preload_through_records(records, reflection, through_association)
188 188 through_records
189 189 end
190 190
191   - # FIXME: quoting
192 191 def preload_belongs_to_association(records, reflection, preload_options={})
193 192 options = reflection.options
194 193 primary_key_name = reflection.primary_key_name
@@ -227,9 +226,19 @@ def preload_belongs_to_association(records, reflection, preload_options={})
227 226
228 227 table_name = klass.quoted_table_name
229 228 primary_key = klass.primary_key
230   - conditions = "#{table_name}.#{primary_key} IN (?)"
  229 + conditions = "#{table_name}.#{connection.quote_column_name(primary_key)} IN (?)"
231 230 conditions << append_conditions(options, preload_options)
232   - associated_records = klass.find(:all, :conditions => [conditions, id_map.keys.uniq],
  231 + column_type = klass.columns.detect{|c| c.name == primary_key}.type
  232 + ids = id_map.keys.uniq.map do |id|
  233 + if column_type == :integer
  234 + id.to_i
  235 + elsif column_type == :float
  236 + id.to_f
  237 + else
  238 + id
  239 + end
  240 + end
  241 + associated_records = klass.find(:all, :conditions => [conditions, ids],
233 242 :include => options[:include],
234 243 :select => options[:select],
235 244 :joins => options[:joins],
7 activerecord/test/cases/inheritance_test.rb
@@ -191,6 +191,13 @@ def test_eager_load_belongs_to_something_inherited
191 191 assert_not_nil account.instance_variable_get("@firm"), "nil proves eager load failed"
192 192 end
193 193
  194 + def test_eager_load_belongs_to_primary_key_quoting
  195 + con = Account.connection
  196 + assert_sql(/\(#{con.quote_table_name('companies')}.#{con.quote_column_name('id')} IN \(1\)\)/) do
  197 + Account.find(1, :include => :firm)
  198 + end
  199 + end
  200 +
194 201 def test_alt_eager_loading
195 202 switch_to_alt_inheritance_column
196 203 test_eager_load_belongs_to_something_inherited

0 comments on commit 76df9fa

Please sign in to comment.
Something went wrong with that request. Please try again.