开发者

How can I load DataMapper objects in an SQLite database with data from a spreadsheet?

开发者 https://www.devze.com 2023-01-05 15:49 出处:网络
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 efficien

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 开发者_C百科User model with fields:

  • Name
  • Age
  • Birthday
  • Hometown


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消