Billy Logan Billy Logan - 4 months ago 6x
Ruby Question

How to run migration generation from console?

I want to try to generate migration inside rails console.

This file is responsible for migration creation (particularly create_migrate_file method):


enter image description here

But, for some reason, in my console I can't call ActiveRecord::Generators::MigrationGenerator class.

enter image description here

NameError: uninitialized constant ActiveRecord::Generators::MigrationGenerator

What can I do in this case?


You need to first require rails/generators and then the file where ActiveRecord::Generators::MigrationGenerator is defined.

require 'rails/generators'
require 'rails/generators/active_record/migration/migration_generator'

# => ActiveRecord::Generators::MigrationGenerator