Reputation: 333
how to custom acts_as_taggable_on?
ive had tags,post_tags,posts(Model:Tag,PostTag,Post) in my database.
i do :gem 'acts-as-taggable-on', '~> 2.2.2' in Gemfile.
when generate migration,i found that it has generated default tables(tags,tagger,tagging).
how to custom the default option to adapt my Model?
Upvotes: 0
Views: 861
Reputation:
All tags are stored in ActsAsTaggableOn::Tag(:id, :name) model and get accessed through ActsAsTaggableOn::Taggable(:id, :tag_id, :taggable_type, :taggable_id, :context etc) model.
If you want to switch to acts-as-taggable and not to lose your previously added tags you should create a migration or rake task after creating acts-as-taggable-on default tables.
Like this:
# In your model
acts_as_taggable_on :post_tags
# Rake task
require 'acts-as-taggable-on'
task :move_tags => [:environment] do
@posts = Post.all
Post.transaction do
Tag.transaction do
@posts.each do |p|
if p.tags.any?
p.update_attributes post_tag_list: p.tags.map(&:name)
p.tags.map(&:destroy)
end
end
end
end
end
# Now, if all is ok, you can drop both old tags and join table and remove associasions from your Post model.
I didn't test this snippet but I've successfully done similar thing before.
Upvotes: 3