Guest User

Untitled

a guest
Dec 11th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. require 'bulk_insert'
  2.  
  3. old_logger = ActiveRecord::Base.logger
  4. ActiveRecord::Base.logger = nil
  5.  
  6. model = WhateverModel.find_by(whatever: 'something')
  7. tag_names = ('a'..'i').to_a.permutation.to_a.shuffle.map(&:join)
  8. tag_names_count = tag_names.count
  9.  
  10. max_id = ActsAsTaggableOn::Tag.maximum(:id)
  11.  
  12. puts "Inserting tags..."
  13. tags = tag_names.map.with_index do |tag_name, i|
  14. { name: tag_name }
  15. end
  16. ActsAsTaggableOn::Tag.bulk_insert(
  17. set_size: 10_000, values: tags
  18. )
  19.  
  20. tag_ids = ActsAsTaggableOn::Tag.where("id > ?", max_id).select(:id).pluck(:id)
  21.  
  22. puts "Inserting taggings..."
  23. taggings = tag_ids.map do |tag_id|
  24. { taggable_id: model.id, tag_id: tag_id, context: 'tags', taggable_type: 'WhateverModel' }
  25. end
  26. ActsAsTaggableOn::Tagging.bulk_insert(
  27. set_size: 10_000, values: taggings
  28. )
  29.  
  30. ActiveRecord::Base.logger = old_logger
Add Comment
Please, Sign In to add comment