Open toobulkeh opened 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
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
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.