How to fix the Rails model naming convention issue
When I started working on my Rails project, I created a model called Superhero
as well as a table called superheroes
. I opened up the console to test my code, so I typed batman = Superhero.create(name: “Batman”)
. After I pressed enter, I got this error message: ActiveRecord::StatementInvalid (Could not find table ‘superheros’)
. I instantly knew there was something wrong with the naming convention because the the correct spelling is actually “superheroes”, not “superheros”. It took me some time to look for a solution, but I finally found an easy fix:
- In my app’s directory, I opened
config/initializers/inflections.rb
- And added this code:
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular ‘superhero’, ‘superheroes’
end
And that’s it, it worked like magic and fixed the rails naming convention. I hope this will help you in building your project as well.