mongoid-tags-arent-hard icon indicating copy to clipboard operation
mongoid-tags-arent-hard copied to clipboard

Add #rename method

Open cblock opened this issue 12 years ago • 3 comments

This method should simply rename a tag: Foo.rename_tags('old tag name', 'new tag name')

And it should work with scoped queries too:

Foo.where(name: 'test').rename_tags('old tag name', 'new tag name')
Foo.where(name: 'test').rename_colors('old tag name', 'new tag name')

cblock avatar Nov 25 '12 12:11 cblock

...just updated the issue description: The proposed syntax now better conforms to the naming convention for different tags.

cblock avatar Nov 25 '12 12:11 cblock

Sounds good. I'm looking forward to the pull request. :)

markbates avatar Nov 25 '12 13:11 markbates

The workaround is to find all records in a collection with the tag to be modified. Add the new tag name. Reject the old tag name. This workaround helps in a variety of situations including renaming, deleting, merging, and splitting tags.

Set these variables old_tag is the one to be modified new_tag is the modified version

Examples Rename: tagg => tag Split: twotags => two tags Merge: badtag => existingtag Delete: tag => ""

Collection.where(tags: old_tag).batch_size(100).each do |doc|
  # Add the new tag to the tags array
  doc.tags << params[:new_tag]
  # Reject the old tag from the tags array
  doc.tags.reject! { |tag| tag == old_tag }
  # Save the change
  doc.save!
end

davesouth avatar Jul 17 '14 03:07 davesouth