views:

14

answers:

1

I have Ruby on Rails app using DataMapper, the database is SQLite, the app is hosted on Heroku. I would like to load the DataBase with data from a spreadsheet, however, I don't know the most efficient way...please help!

As an example, let say I have a User model with fields:

  • Name
  • Age
  • Birthday
  • Hometown
A: 

I had a similar problem of importing external data into datamapper. I did a CSV dump of the data from the external database, then wrote an import which read the CSV and create a new record.

class Staff
include DataMapper::Resource
  property :id, String, :key => true 
  property  :full_name, String 
  property  :email, String
  has n, :stages
end

Then:

CSV.parse(staff) do |row|

@staff = Staff.create(
  :id => row[1],
  :full_name => row[0],
  :email => row[0].downcase.gsub!(' ', '.')
);


@staff.save

Perhaps an approach like this would be suitable?

Tom
Thanks this is very elegant!
hagope