Skip to content

Commit

Permalink
fixed association preloading to use = instead of IN when there's only…
Browse files Browse the repository at this point in the history
… one record

[#1013 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
rsl authored and jeremy committed Sep 10, 2008
1 parent 5bbca48 commit 6ce1342
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 8 additions & 5 deletions activerecord/lib/active_record/association_preload.rb
Expand Up @@ -95,7 +95,7 @@ def preload_has_and_belongs_to_many_association(records, reflection, preload_opt
records.each {|record| record.send(reflection.name).loaded}
options = reflection.options

conditions = "t0.#{reflection.primary_key_name} IN (?)"
conditions = "t0.#{reflection.primary_key_name} #{in_or_equals_for_ids(ids)}"
conditions << append_conditions(options, preload_options)

associated_records = reflection.klass.find(:all, :conditions => [conditions, ids],
Expand Down Expand Up @@ -222,8 +222,6 @@ def preload_belongs_to_association(records, reflection, preload_options={})

table_name = klass.quoted_table_name
primary_key = klass.primary_key
conditions = "#{table_name}.#{connection.quote_column_name(primary_key)} IN (?)"
conditions << append_conditions(options, preload_options)
column_type = klass.columns.detect{|c| c.name == primary_key}.type
ids = id_map.keys.uniq.map do |id|
if column_type == :integer
Expand All @@ -234,6 +232,8 @@ def preload_belongs_to_association(records, reflection, preload_options={})
id
end
end
conditions = "#{table_name}.#{connection.quote_column_name(primary_key)} #{in_or_equals_for_ids(ids)}"
conditions << append_conditions(options, preload_options)
associated_records = klass.find(:all, :conditions => [conditions, ids],
:include => options[:include],
:select => options[:select],
Expand All @@ -248,10 +248,10 @@ def find_associated_records(ids, reflection, preload_options)
table_name = reflection.klass.quoted_table_name

if interface = reflection.options[:as]
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}'"
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}'"
else
foreign_key = reflection.primary_key_name
conditions = "#{reflection.klass.quoted_table_name}.#{foreign_key} IN (?)"
conditions = "#{reflection.klass.quoted_table_name}.#{foreign_key} #{in_or_equals_for_ids(ids)}"
end

conditions << append_conditions(options, preload_options)
Expand All @@ -277,6 +277,9 @@ def append_conditions(options, preload_options)
sql
end

def in_or_equals_for_ids(ids)
ids.size > 1 ? "IN (?)" : "= ?"
end
end
end
end
2 changes: 1 addition & 1 deletion activerecord/test/cases/inheritance_test.rb
Expand Up @@ -193,7 +193,7 @@ def test_eager_load_belongs_to_something_inherited

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
assert_sql(/\(#{con.quote_table_name('companies')}.#{con.quote_column_name('id')} = 1\)/) do
Account.find(1, :include => :firm)
end
end
Expand Down

0 comments on commit 6ce1342

Please sign in to comment.