Skip to content

Instantly share code, notes, and snippets.

@koffeinfrei
Last active December 11, 2018 09:00
Show Gist options
  • Save koffeinfrei/5dc937a3b469abebc5f848ea4894e756 to your computer and use it in GitHub Desktop.
Save koffeinfrei/5dc937a3b469abebc5f848ea4894e756 to your computer and use it in GitHub Desktop.
generate a lot of random ActsAsTaggableOn::Tag
require 'bulk_insert'
old_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = nil
model = WhateverModel.find_by(whatever: 'something')
tag_names = ('a'..'i').to_a.permutation.to_a.shuffle.map(&:join)
tag_names_count = tag_names.count
max_id = ActsAsTaggableOn::Tag.maximum(:id)
puts "Inserting tags..."
tags = tag_names.map.with_index do |tag_name, i|
{ name: tag_name }
end
ActsAsTaggableOn::Tag.bulk_insert(
set_size: 10_000, values: tags
)
tag_ids = ActsAsTaggableOn::Tag.where("id > ?", max_id).select(:id).pluck(:id)
puts "Inserting taggings..."
taggings = tag_ids.map do |tag_id|
{ taggable_id: model.id, tag_id: tag_id, context: 'tags', taggable_type: 'WhateverModel' }
end
ActsAsTaggableOn::Tagging.bulk_insert(
set_size: 10_000, values: taggings
)
ActiveRecord::Base.logger = old_logger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment