Use ActiveRecord Column#type_cast to properly parse fields represented by a string.
Fixed issued reported by Nick Burdick where imported datetime fields was not correctly working with certain string formats for MySQL which doesn't support MM/DD/YYYY format directly.
This commit is contained in:
parent
8e266f74f7
commit
8fbf841fde
|
@ -129,7 +129,8 @@ module ActiveRecord::Import::AbstractAdapter
|
||||||
array_of_attributes.each do |arr|
|
array_of_attributes.each do |arr|
|
||||||
my_values = []
|
my_values = []
|
||||||
arr.each_with_index do |val,j|
|
arr.each_with_index do |val,j|
|
||||||
my_values << quote( val, columns[j] )
|
importable_value = columns[j].type_cast(val)
|
||||||
|
my_values << quote(importable_value, columns[j] )
|
||||||
end
|
end
|
||||||
values << my_values
|
values << my_values
|
||||||
end
|
end
|
||||||
|
|
|
@ -257,7 +257,7 @@ class ActiveRecord::Base
|
||||||
def import_without_validations_or_callbacks( column_names, array_of_attributes, options={} )
|
def import_without_validations_or_callbacks( column_names, array_of_attributes, options={} )
|
||||||
escaped_column_names = quote_column_names( column_names )
|
escaped_column_names = quote_column_names( column_names )
|
||||||
columns = []
|
columns = []
|
||||||
array_of_attributes.first.each_with_index { |arr,i| columns << columns_hash[ column_names[i] ] }
|
array_of_attributes.first.each_with_index { |arr,i| columns << columns_hash[ column_names[i].to_s ] }
|
||||||
|
|
||||||
if not supports_import?
|
if not supports_import?
|
||||||
columns_sql = "(" + escaped_column_names.join( ',' ) + ")"
|
columns_sql = "(" + escaped_column_names.join( ',' ) + ")"
|
||||||
|
|
|
@ -199,4 +199,16 @@ describe "#import" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "importing a datetime field" do
|
||||||
|
it "should import a date with MM/DD/YYYY format just fine" do
|
||||||
|
Topic.import [:author_name, :title, :last_read], [["Bob Jones", "Topic 1", "05/14/2010"]]
|
||||||
|
assert_equal "05/14/2010".to_date, Topic.last.last_read.to_date
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should import a date with YYYY/MM/DD format just fine" do
|
||||||
|
Topic.import [:author_name, :title, :last_read], [["Bob Jones", "Topic 2", "2010/05/14"]]
|
||||||
|
assert_equal "05/14/2010".to_date, Topic.last.last_read.to_date
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
Loading…
Reference in a new issue