Migrating to acts_as_taggable_on from acts_as_taggable_on_steroids
The Acts As Taggable On Steroids plugin for Rails has certainly been around the block a few times, I indeed have been using it in many of my projects for a few years. Now the time has come however where the lack of named_scope support is more than a minor irritation, and its time to move on: Acts As Taggable On is a fine replacement with all the named scope magic and additional context support which might be useful in some situations.
Out of the box, the new plugin is not table compatible with the old, so if you have an older project you need to upgrade without loosing the old taggings we need to perform a migration. tkwong mentioned a similar process, but there seems to be the solution wasn’t very clear to me, so I wrote my own.
Firstly, you need to grab the plugin and move the old one out the way:
script/plugin install git://github.com/mbleigh/acts-as-taggable-on.git mv vendor/plugins/acts_as_taggable_on_steroids /tmp
Then we add a new empty migration to handle the table changes:
script/generate migration acts_as_taggable_on_migration
The contents of the migration should look like the following:
class ActsAsTaggableOnMigration < ActiveRecord::Migration
def self.up
remove_column :tags, :reference_count
rename_column :taggings, :created_on, :created_at
add_column :taggings, :tagger_id, :integer
add_column :taggings, :tagger_type, :string
add_column :taggings, :context, :string
remove_index :tags, :name
remove_index :taggings, [:taggable_id,:taggable_type]
add_index :taggings, [:taggable_id, :taggable_type, :context]
Tag.connection.execute("UPDATE taggings SET context = 'tags'")
end
def self.down
end
end
I’ve been lazy and not included the reverse migration to save a bit of space. Note the last bit of manual SQL code, this will ensure all of the old tagging associations are defined in the :tags context.
All the old models using the old acts_as_taggable call can be modified to the following:
acts_as_taggable_on :tags
And thats it! You can now fully enjoy the benefits of named scopes:
Artifact.tagged_with('rosas', :on => :tags).paginate :page => 1, :per_page => 10
Fantastic!
No comments yet