heartcombo / devise

Flexible authentication solution for Rails with Warden.
http://blog.plataformatec.com.br/tag/devise/
MIT License
24k stars 5.55k forks source link

NameError: uninitialized constant ApplicationRecord #5379

Closed nightfall708 closed 3 years ago

nightfall708 commented 3 years ago

Pre-check

Environment

Current behavior

I've installed devise and generated the migration. However I'm encountering this error when trying to migrate:

NameError: uninitialized constant ApplicationRecord /root/myapp/app/models/user.rb:1:in `<top (required)>'

Not sure why, because ApplicationRecord should be supported in rails 6.

seuros commented 3 years ago

do you have a file app/models/application_record.rb ?

PS : coding as root is very dangerous.

carlosantoniodasilva commented 3 years ago

Also: does it work without Devise, and fails only with it?

The error doesn't seem to mention or point to anything related to Devise apparently, so I'm inclined to think it's unrelated to the library. If you are able to reproduce just with Devise and can send some script or app reproducing the problem, I'll be happy to take a better look. Thanks.

nightfall708 commented 3 years ago

@seuros I don't have that file.. @carlosantoniodasilva the user.rb file was generated after creating the devise migration (rails g devise User) so it's related...

carlosantoniodasilva commented 3 years ago

@talshr devise triggers the Rails / Active Record generator to create the model if it doesn't exist: https://github.com/heartcombo/devise/blob/c82e4cf47b02002b2fd7ca31d441cf1043fc634c/lib/generators/active_record/devise_generator.rb#L25

And that should create a model inheriting from ApplicationRecord (instead of the "old" way from ActiveRecord::Base): https://github.com/rails/rails/blob/255b5ff9af57f9b54dee7ec884b12a1ad16f0321/activerecord/lib/rails/generators/active_record/model/model_generator.rb#L57

If you don't have ApplicationRecord in your app its probably because it's been upgraded over time and never created one, so other models are likely inheriting from ActiveRecord::Base... You can either just change the user model to inherit from it too, or create an ApplicationRecord as that's expected to exist going forward. You can do that via the active_record:application_record generator. Hope that helps.

nightfall708 commented 3 years ago

Yes already solved this way thank you!

MR-RenatoBlue commented 3 years ago

Thanks. This helped me too. I had the same error because I've added "devise_for :users" in config/routes.rb before running $ rails generate devise User

pranshujainbandgi commented 7 months ago

Upon upgrading from Rails 6 to 7 and Ruby 2.5.6 to 3.0.0, I encountered the same issue while setting up the project. , when running rails c, I received the following error: /home/pranshu_007/edn_server/app/models/configuration.rb:17:in <top (required)>': uninitialized constant ApplicationRecord (NameError) from /home/pranshu_007/.rvm/gems/ruby-3.0.0/gems/zeitwerk-2.6.13/lib/zeitwerk/kernel.rb:34:inrequire' from /home/pranshu_007/.rvm/gems/ruby-3.0.0/gems/zeitwerk-2.6.13/lib/zeitwerk/kernel.rb:34:in require' from /home/pranshu_007/edn_server/config/initializers/carrierwave.rb:6:in<top (required)>' from /home/pranshu_007/.rvm/gems/ruby-3.0.0/gems/railties-7.1.0/lib/rails/engine.rb:683:in load' from /home/pranshu_007/.rvm/gems/ruby-3.0.0/gems/railties-7.1.0/lib/rails/engine.rb:683:inblock in load_config_initializer' from /home/pranshu_007/.rvm/gems/ruby-3.0.0/gems/activesupport-7.1.0/lib/active_support/notifications.rb:208:in instrument' from /home/pranshu_007/.rvm/gems/ruby-3.0.0/gems/railties-7.1.0/lib/rails/engine.rb:682:inload_config_initializer' from /home/pranshu_007/.rvm/gems/ruby-3.0.0/gems/railties-7.1.0/lib/rails/engine.rb:636:in block (2 levels) in <class:Engine>' from /home/pranshu_007/.rvm/gems/ruby-3.0.0/gems/railties-7.1.0/lib/rails/engine.rb:635:ineach' from /home/pranshu_007/.rvm/gems/ruby-3.0.0/gems/railties-7.1.0/lib/rails/engine.rb:635:in block in <class:Engine>' from /home/pranshu_007/.rvm/gems/ruby-3.0.0/gems/railties-7.1.0/lib/rails/initializable.rb:32:ininstance_exec' from /home/pranshu_007/.rvm/gems/ruby-3.0.0/gems/railties-7.1.0/lib/rails/initializable.rb:32:in run' from /home/pranshu_007/.rvm/gems/ruby-3.0.0/gems/railties-7.1.0/lib/rails/initializable.rb:61:inblock in run_initializers' from /home/pranshu_007/.rbenv/versions/3.0.0/lib/ruby/3.0.0/tsort.rb:228:in block in tsort_each' from /home/pranshu_007/.rbenv/versions/3.0.0/lib/ruby/3.0.0/tsort.rb:350:inblock (2 levels) in each_strongly_connected_component' from /home/pranshu_007/.rbenv/versions/3.0.0/lib/ruby/3.0.0/tsort.rb:422:in `block (2 levels) in each_strongly_connected_component_from'