ruby批量插入数据,bulk_insert-----Gem包使用

 

文档

https://github.com/jamis/bulk_insert

class Book < ActiveRecord::Base
end

book_attrs = ... # some array of hashes, for instance
Book.bulk_insert do |worker|
  book_attrs.each do |attrs|
    worker.add(attrs)
  end
end

 

#向数据库批量插入数据
"d_data_yyyymm_params"=>[{"station_id"=>1, "item_code"=>107, "data_cycle"=>300, "data_attr"=>"T", "data_time"=>201702160905, "data_value"=>148, "data_label"=>""},
{"station_id"=>1, "item_code"=>108, "data_cycle"=>300, "data_attr"=>"T", "data_time"=>201702160905, "data_value"=>148, "data_label"=>""}]


DDataYyyymm.bulk_insert(update_duplicates: true) do |worker|
d_data_yyyymm_params.each do |fm|
worker.add(fm)
end
end

##之前 以后不要出现这种插入数据
d_data_yyyymm_params.each do |fm|
d_data = DDataYyyymm.new
d_data.station_id = fm.station_id
d_data.item_code = fm.item_code


d_data.save
end

posted @ 2018-06-19 17:14  lmg6406  阅读(589)  评论(0编辑  收藏  举报