# desc "Explaining what the task does" # task :video_pro do # # Task goes here # end namespace :video_pro_tasks do desc "Migrate old tags to new tags" task :migrate_hashtags => :environment do module_app = ModuleApp.find_by_key("video_pro") dataset = {} VideoTag.all.each do |vt| next if vt.title.blank? # Skip if tag name is blank next if vt.video_images.empty? # Skip if no images are associated with the tag puts "Processing tag: #{vt.title}" module_app.reload tag_str = vt.title.gsub(/^#+/, '').downcase.strip hashtag = module_app.hashtags.where(name: tag_str).first_or_initialize hashtag.module_app_ids << module_app.id.to_s unless hashtag.module_app_ids.include?(module_app.id.to_s) puts "New record #{hashtag.new_record?}" hashtag.save if hashtag.new_record? puts "Hashtag ID: #{hashtag.id} - #{hashtag.name}" vt.video_images.each do |vi| unless vi.hashtags.include?(hashtag) dataset[vi] ||= [] dataset[vi] << hashtag.id end end end dataset.each do |vi, tag_ids| puts "Updating TableEntry #{vi.id} with tags: #{tag_ids.join(', ')}" vi.hashtags= tag_ids vi.save end end end