Rails 3 Generators: Adding Migration Templates

I’ve been working on some really cool projects using Ruby on Rails recently, but I found myself duplicating code across them. Obviously, that doesn’t make sense to me, even when the code bases are different, so I decided to start extracting some of the logic into Ruby’s awesome gem format. Rails 3 has awesome gem handling, if I’ve found uses for some of these pieces of functionality across multiple projects, then others may as well.

The first gem I’m working on stems from this post from a couple years ago where I demonstrated how to do dynamically-named routes using semi-static pages. This approach was different than ThoughtBot’s high_voltage gem because it stored the page content in the database rather than as files in the database deployment. I have an issue with that implementation because I’m a firm believer that you shouldn’t have to deploy a new revision of your software just to update a bit of content; it should be controllable from the user interface as part of our regular CRUD routine.

My first struggle with the creation of the gem was writing a template for the migration. I had some trouble with these two errors coming up:

  • 1
    undefined method `migration_template' for #<Semisonic::InstallGenerator:0x000000040899c8> (NoMethodError)
  • 1
    `next_migration_number': NotImplementedError (NotImplementedError)

Maybe my Google skills have been slipping, but I had trouble finding a ‘fix’ for these. So, here’s how to fix the above errors if you ever get them:

  • If you get the first error (undefined method ‘migration_template’), you’re missing an include in your class. Add “include Rails::Generators::Migration” on the first line of your generator class, and you’re good to go!
  • If you get the second error (‘next_migration_number’ not implemented), you’ll need to implement the method ‘next_migration_number’. The following code snippet should work just fine for it:
    def self.next_migration_number(path)
      unless @prev_migration_nr
        @prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
        @prev_migration_nr += 1

Hopefully those two fixes help somebody else out.

One thought on “Rails 3 Generators: Adding Migration Templates”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.