Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix integer quoting issues in association preload. [#602 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
Tiago Macedo authored and lifo committed Jul 14, 2008
1 parent cd9b242 commit 76df9fa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
15 changes: 12 additions & 3 deletions activerecord/lib/active_record/association_preload.rb
Expand Up @@ -188,7 +188,6 @@ def preload_through_records(records, reflection, through_association)
through_records
end

# FIXME: quoting
def preload_belongs_to_association(records, reflection, preload_options={})
options = reflection.options
primary_key_name = reflection.primary_key_name
Expand Down Expand Up @@ -227,9 +226,19 @@ def preload_belongs_to_association(records, reflection, preload_options={})

table_name = klass.quoted_table_name
primary_key = klass.primary_key
conditions = "#{table_name}.#{primary_key} IN (?)"
conditions = "#{table_name}.#{connection.quote_column_name(primary_key)} IN (?)"
conditions << append_conditions(options, preload_options)
associated_records = klass.find(:all, :conditions => [conditions, id_map.keys.uniq],
column_type = klass.columns.detect{|c| c.name == primary_key}.type
ids = id_map.keys.uniq.map do |id|
if column_type == :integer
id.to_i
elsif column_type == :float
id.to_f
else
id
end
end
associated_records = klass.find(:all, :conditions => [conditions, ids],
:include => options[:include],
:select => options[:select],
:joins => options[:joins],
Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/inheritance_test.rb
Expand Up @@ -191,6 +191,13 @@ def test_eager_load_belongs_to_something_inherited
assert_not_nil account.instance_variable_get("@firm"), "nil proves eager load failed"
end

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
Account.find(1, :include => :firm)
end
end

def test_alt_eager_loading
switch_to_alt_inheritance_column
test_eager_load_belongs_to_something_inherited
Expand Down

0 comments on commit 76df9fa

Please sign in to comment.