我写了一个gem,如果你传入一个ActiveRecord模型,它会将数据导入你的数据库.例如:
importer = Importer.new(Widget) importer.import(data_source)
解决方法
大多数灵感来自这篇文章:
http://blog.markstarkman.com/blog/2013/01/23/using-sqlite-to-test-active-record-models/
http://blog.markstarkman.com/blog/2013/01/23/using-sqlite-to-test-active-record-models/
首先,在您的gemspec中,您可以添加ActiveRecord和sqlite3作为依赖项,如下所示:
spec.add_development_dependency "activerecord","~> 4.0.0" spec.add_development_dependency "sqlite3"
然后在spec / schema.rb中,您可以像这样定义您的模式:
ActiveRecord::Schema.define do self.verbose = false create_table :users,:force => true do |t| t.string :key t.string :name t.integer :age t.datetime :dob t.timestamps end end
然后,您可以在models.rb文件中创建模型:
class User < ActiveRecord::Base end
在spec_helper.rb中,您希望连接到内存中的sqlite数据库,加载模式并需要模型:
require 'active_record' ActiveRecord::Base.establish_connection adapter: "sqlite3",database: ":memory:" load File.dirname(__FILE__) + '/schema.rb' require File.dirname(__FILE__) + '/models.rb'