`
苍long破
  • 浏览: 2002 次
  • 性别: Icon_minigender_1
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

activerecord-import批量处理数据

阅读更多
#database.yml
adapter:  mysql
database: mytest 
username: username 
password: password
#------------------------------------
#conn.rb
#encoding:utf-8
require 'rubygems'   
require 'active_record'   
require 'yaml'     
require 'logger'

dbconfig = YAML::load(File.open('database.yml'))   
puts dbconfig
ActiveRecord::Base.establish_connection(dbconfig)   
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.logger = Logger.new(File.open('database.log','a'))
class Book < ActiveRecord::Base  
  set_table_name "activerecord-import_test"#自己建一个表,添加一个name字段
end 
#-----------------------------
#activerecord-import_test.rb

require 'active_record'
require 'activerecord-import'
require './conn.rb'
books = []
10000.times do |i|
  books << Book.new(:name => "book #{i}")
end
puts Time.now.to_i
Book.import books
puts Time.now.to_i


10000.times do |i|
  Book.create! :name => "book #{i}"
end
puts Time.now.to_i

#----------------------------------------
参考链接:
activerecord-import  api:http://rubydoc.info/gems/activerecord-import/0.2.9/ActiveRecord/Base#establish_connection_with_activerecord_import-class_method
使用activerecord-import 批量导入数据至mysql on rails enhanced active-record:http://archive.cnblogs.com/a/1913214/
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics