Debug Output For Migrations in Ruby on Rails 4

Posted by Ben Reubenstein Tue, 31 Jul 2007 13:04:00 GMT

Today I was doing some massive migrations that rocked the very foundations of an app I am working on. Problem was that during the migration I was moving objects around the database and validations were failing for my saves. To get some debugging going, I started out trying to write to the logger, with some good ol' logger.debug, but that does not work within a migration. To output information in a migration, use puts. So for example to see all the errors that prevented object g from being saved:
class MyMigration < ActiveRecord::Migration
  def self.up
    g = G.new(:name => "name" )
    if g.save
      #More object manipulation here
    else
      g.errors.each do |x|
        puts x
      end
    end
  end

  def self.down
    #Down Method Code Here
  end
end