How to create Rails 3 migration generator?


I’d like to add Rails 3 compatible generator to acts_as_taggable_on
gem (all it does is call “migration_template” method) and got a
problem with “next_migration_number” method not being implemented.

For ActiveRecord this method is defined in
ActiveRecord::Generators::Base class, which inherits from NamedBase
class. How can I use it inside my generator, which inherits from
Rails::Generators::Base? How to use “hook_for :orm” here, so that the
proper “next_migration_number” method definition is be used?


If you look at the scaffold generator, it is able to apply the correct
model/migration generator depending on the orm setting in
This is handled transparently as I see it. It simply calls the correct
migration and model generators with a name corresponding to the orm
Check out my for an
example. This can be used by
Which contains a scaffold generator. This calls the mongo_mapper
generator if orm is set to :mongo_mapper and so on…
Something similar should be possible with migrations.

Also see: for more details :wink:

Good luck!


Thanks, but the problem is that if I add the following line to my

hook_for :orm, :as => “migration”

it invokes i.e. ActiveRecord::Generators::Migration and it says that
the name attribute is missing, probably because AR::G::Migration
inherits from AR::G::Base, which inherits from
Rails::Generators::NamedBase. I don’t want to specify migration name,
as it’s already hardcoded in my generator, I just want to add a proper
prefix (i.e. timestamp), which is added by the “next_migration_number”

All I need for my generator is to be able to access
“migration_template” method and “next_migration_number” depending on
ORM selected. It works fine if I simply copy “next_migration_number”
method from ActiveRecord::Generators::Base, but I doubt it’s the
proper way to do it :slight_smile:

Here’s how it looks currently:

BTW. Are there Rails 3 API docs available anywhere? I can’t generate
my own, because I got the following error: “wrong constant name ./DOC/

So the solution for now is to make the “next_migration_number” method
inside my generator to call a class method.

However, assuming that generators for other ORMs like DataMapper or
Sequel also follow this convention, how can I check which ORM is used
by the application, so that I can change the path to the template and
call the proper ActiveModel::Generarators::Base.next_migration_number

It should be Rails::Generators::Migration.next_migration_number, not
ActiveModel::Generarators::Base.next_migration_number :slight_smile:

If you have a conventional migration, you can invoke the migration
generator explicitly:

class ActsAsTaggableOnMigrationGenerator < Rails::Generators::Base
invoke “migration”, %(add_fields_to_tags name:string label:string)

However, if you need a customized template, currently you have to copy
the logic that gets the migration number. Maybe we could expose that
as a class method, so you just need to do:


Feel free to work in a patch and assign to me when it’s done. If not
in mood for a patch, please create a ticket on Lighthouse and assign
to me, so I can tackle it later.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs