bulk insert/delete in Rails3/4 (Rails3/4中的批量删除)

访问量: 1854

批量删除: (bulk delete) 

# if a plan has 100K target_urls:   (如果一个plan 有 100K 个 target_urls ) 
# never use:   (绝对不要这样做) 
#@plan.target_urls.clear
# you should use: 
TargetUrl.delete_all(:plan_id => @plan.id) 

批量增加:  使用 gem: activerecord-import

1. in Gem file: 
gem 'activerecord-import', '~> 0.5.0'

2. in controller:
   
    target_url_entities = []
    urls.each do |url_to_request|
      target_url_entities << TargetUrl.new(:url => url_to_request, :plan_id => @plan.id)
    end 
    TargetUrl.import target_url_entities, :validate => false

refer to: https://github.com/zdennis/activerecord-import/wiki/Examples

订阅/RSS Feed

Subscribe

分类/category