Mat Schaffer on 7 Mar 2007 18:46:58 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PhillyOnRails] importing from csv



On Mar 7, 2007, at 12:08 PM, Flinn Mueller wrote:

Anyone have a spectacular method for importing data from CSV for ActiveRecord? Thanks -- flinn

A few things come to mind. First, Rails fixtures can support CSV (http://api.rubyonrails.com/classes/Fixtures.html). So if you had it in a fixture file in the right place:


> rake db:import:fixtures FIXTURES=table_name

My curiousity caused me to dig a bit and find out that you could load arbitrary files pragmatically with:

require 'active_record/fixtures' # not included by default in ./ script/console for me
Fixtures.create_fixtures(fixtures_directory, table_name)


Finally there's always the more down and dirty approach which would allow some manipulation before importing:

FasterCSV.foreach(file, :headers => :first_row) do |row|
	ModelName.create(row.to_hash)
end

-Mat
_______________________________________________
To unsubscribe or change your settings, visit:
http://lists.phillyonrails.org/mailman/listinfo/talk