class CartoonCharacter < ActiveRecord::Base
#values for NEW objects
names = ["Tom", "Jerry", "Donald"]
#loop thru and create objects
names.each do |name|
CartoonCharacter.create!(:name => name)
It's not as bigger problem if you're just creating 2 or 3 records. Imagine you want to create 70,000 records. I was required to do the same. This approach killed my machine and it just took ages. I couldn't wait after 5 minutes and killed the process.
I started looking out for other options. I came across ar-extensions.
It is a nice gem for mass import and conversion of data to CSV. It also incorporates better finder support. All I've used is its mass import functionality.
column_names = [:name, :age, :country]
values = 
#build up values
values << ["Tom", "65", "AU"]
CartoonCharacter.import column_names, values
That's it. It worked like a charm. Yes, please don't forget to include adapter specific functionality.
Recently, some work has been done to extract the import functionality of ar-extenstions into activerecord-import.
More information available at: http://continuousthinking.com/tags/arext