'NameError: uninitialized constant when running migration that has an inflected name

I have this migration file 20180226063342_add_bpm_to_songs.rb

class AddBpmToSongs < ActiveRecord::Migration[5.1]
  def change
    ..
  end
end

and this in my inflections.rb

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym 'BPM'
end

When I run rails db:migrate I get

** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
rails aborted!
NameError: uninitialized constant AddBPMToSongs
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.1/lib/active_support/    inflector/methods.rb:269:in `const_get'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.1/lib/active_support/    inflector/methods.rb:269:in `block in constantize'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.1/lib/active_support/    inflector/methods.rb:267:in `each'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.1/lib/active_support/    inflector/methods.rb:267:in `inject'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.1/lib/active_support/    inflector/methods.rb:267:in `constantize'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.1/lib/active_support/    core_ext/string/inflections.rb:66:in `constantize'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:963:in `load_migration'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:958:in `migration'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:953:in `disable_ddl_transaction'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:1305:in `use_transaction?'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:1235:in `rescue in execute_migration_in_transaction'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:1224:in `execute_migration_in_transaction'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:1201:in `block in migrate_without_lock'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:1200:in `each'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:1200:in `migrate_without_lock'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:1148:in `block in migrate'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:1317:in `with_advisory_lock'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:1148:in `migrate'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:1007:in `up'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:985:in `migrate'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/tasks/    database_tasks.rb:171:in `migrate'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/railties/    databases.rake:58:in `block (2 levels) in <top (required)>'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/task.rb:250:in `block in     execute'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/task.rb:250:in `each'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/task.rb:250:in `execute'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/task.rb:194:in `block in     invoke_with_call_chain'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/task.rb:187:in     `invoke_with_call_chain'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/task.rb:180:in `invoke'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:152:in     `invoke_task'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:108:in     `block (2 levels) in top_level'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:108:in     `each'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:108:in     `block in top_level'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:117:in     `run_with_threads'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:102:in     `top_level'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.1/lib/rails/commands/rake/    rake_command.rb:21:in `block in perform'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:178:in     `standard_exception_handling'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.1/lib/rails/commands/rake/    rake_command.rb:18:in `perform'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.1/lib/rails/command.rb:46:in     `invoke'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.1/lib/rails/commands.rb:16:in `<    top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
NameError: uninitialized constant AddBPMToSongs
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.1/lib/active_support/    inflector/methods.rb:269:in `const_get'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.1/lib/active_support/    inflector/methods.rb:269:in `block in constantize'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.1/lib/active_support/    inflector/methods.rb:267:in `each'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.1/lib/active_support/    inflector/methods.rb:267:in `inject'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.1/lib/active_support/    inflector/methods.rb:267:in `constantize'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.1/lib/active_support/    core_ext/string/inflections.rb:66:in `constantize'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:963:in `load_migration'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:958:in `migration'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:953:in `disable_ddl_transaction'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:1305:in `use_transaction?'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:1297:in `ddl_transaction'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:1229:in `execute_migration_in_transaction'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:1201:in `block in migrate_without_lock'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:1200:in `each'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:1200:in `migrate_without_lock'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:1148:in `block in migrate'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:1317:in `with_advisory_lock'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:1148:in `migrate'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:1007:in `up'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/    migration.rb:985:in `migrate'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/tasks/    database_tasks.rb:171:in `migrate'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.1/lib/active_record/railties/    databases.rake:58:in `block (2 levels) in <top (required)>'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/task.rb:250:in `block in     execute'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/task.rb:250:in `each'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/task.rb:250:in `execute'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/task.rb:194:in `block in     invoke_with_call_chain'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/task.rb:187:in     `invoke_with_call_chain'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/task.rb:180:in `invoke'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:152:in     `invoke_task'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:108:in     `block (2 levels) in top_level'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:108:in     `each'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:108:in     `block in top_level'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:117:in     `run_with_threads'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:102:in     `top_level'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.1/lib/rails/commands/rake/    rake_command.rb:21:in `block in perform'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rake-12.0.0/lib/rake/application.rb:178:in     `standard_exception_handling'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.1/lib/rails/commands/rake/    rake_command.rb:18:in `perform'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.1/lib/rails/command.rb:46:in     `invoke'
/Users/redacted/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.1/lib/rails/commands.rb:16:in `<    top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate

I know that it has to do with the BPM inflection because if I remove it, then the migration goes through successfully. However, I really want to keep that inflection.

Does anyone know why the migrations file is getting inflected improperly and what I can do about it?



Solution 1:[1]

First you need to seed Rails environment so please don't forget set RAILS_ENV=production in front of CLI commands.

These inflection rules (acronym) are supported but not enabled by default:

ActiveSupport::Inflector.inflections(:en) do |inflect|
   inflect.acronym 'BPM'
end

Acronyms that are passed to pluralize will no longer be recognized, since the acronym will not occur as a delimited unit in the pluralized result. To work around this, you must specify the pluralized form as an acronym as well:

acronym 'BPM'
camelize(pluralize('bpm')) # => 'bpm'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yogesh Waghmare