Fix that values_sql_for_attributes wasn't accessing the columns with the same indexes as the array_of_attributes, and so was sometime mismatched.

Also return to the column_names[j] == primary_key style of testing, as the column itself is not always present
This commit is contained in:
Ben Woosley 2010-11-17 00:19:18 -08:00 committed by Zach Dennis
parent df75eea4db
commit 7bd0e2693a

View file

@ -256,7 +256,7 @@ class ActiveRecord::Base
columns = column_names.map { |name| columns_hash[name.to_s] }
columns_sql = "(#{column_names.map{|name| connection.quote_column_name(name) }.join(',')})"
values_sql = values_sql_for_attributes(array_of_attributes)
values_sql = values_sql_for_column_names_and_attributes(column_names, array_of_attributes)
if not supports_import?
number_inserted = 0
values_sql.each do |values|
@ -280,10 +280,12 @@ class ActiveRecord::Base
# Returns SQL the VALUES for an INSERT statement given the passed in +columns+
# and +array_of_attributes+.
def values_sql_for_attributes(array_of_attributes ) # :nodoc:
def values_sql_for_column_names_and_attributes(column_names, array_of_attributes) # :nodoc:
columns = column_names.map { |name| columns_hash[name.to_s] }
array_of_attributes.map do |arr|
my_values = arr.each_with_index.map do |val,j|
if !sequence_name.blank? && columns[j].primary && val.nil?
if !sequence_name.blank? && column_names[j] == primary_key && val.nil?
connection.next_value_for_sequence(sequence_name)
else
connection.quote(val, columns[j])