mbleigh / acts-as-taggable-on

A tagging plugin for Rails applications that allows for custom tagging along dynamic contexts.
http://mbleigh.lighthouseapp.com/projects/10116-acts-as-taggable-on
MIT License
4.98k stars 1.19k forks source link

Mass create Tags on objects #856

Open toobulkeh opened 7 years ago

toobulkeh commented 7 years ago

Is there a recommended way of mass-adding tags to collections of objects? We have about 20K entries that need a new tag as we migrate some data.

I've tried to directly create the Tagging objects, but get errors on validation that don't really make sense.

jasonfb commented 7 years ago

run a background job. We use something like this. (there is calling code you can't see here, so you can use your imagination as to how this method is called)

  def import(file_path, options)
    tag_string = options[:tag]

    tag = ActsAsTaggableOn::Tag.find_by(name: tag_string)
    if !tag
      return {success: false, results: "can't find tag #{tag_string}"}
    end

    res = ""
    CSV.foreach(file_path) do |row|
      user_email = row[0]
      user = User.find_by(email: user_email)
      if !user
        res << "Can't find #{user_email} \n"
        next
      end
      user.tag_list.add(tag_string)
      user.save
      res << "Adding tag #{tag_string} to user #{user_email} ... \n"

    end

    return {success: true, results: res}
  end
conwayanderson commented 5 years ago

Curious if you ever found a better way. Also, is there a way to do this in the console?

I tried this but got NameError on the tag: Candidate.all.each do |candidate|; candidate.tag_list.add(“Original”); candidate.save; end