[英]NameError: uninitialized constant ApplicationRecord despite having the application_record file correctly
I am having trouble using my actual models, I am getting the following error when trying to create a user:我在使用实际模型时遇到问题,尝试创建用户时出现以下错误:
app/models/user.rb:1:in `<top (required)>': uninitialized constant ApplicationRecord (NameError)
I do have the following file in models/application_record.rb我在 models/application_record.rb 中有以下文件
which looks like:看起来像:
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
When I change the User class to inherit from ActiveRecord::Base instead, I get the same error, except for ActiveRecord.当我将 User 类更改为从 ActiveRecord::Base 继承时,我得到相同的错误,除了 ActiveRecord。
My application.rb:我的应用程序.rb:
class Application < Rails::Application
config.load_defaults 6.1
config.generators.system_tests = nil
end
I am using Rails 6.1.我正在使用 Rails 6.1。
My Gemfile:我的 Gemfile:
ruby '2.6.3'
gem 'activerecord'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
gem 'rails', '~> 6.1.4'
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '~> 1.4'
# Use Puma as the app server
gem 'puma', '~> 5.0'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 5.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.4', require: false
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 4.1.0'
# Display performance information such as SQL time and flame graphs for each request in your browser.
# Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md
gem 'rack-mini-profiler', '~> 2.0'
gem 'listen', '~> 3.3'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
What is happening?怎么了? I have no spelling mistakes, the model is user.rb, I can't figure out what's going on.我没有拼写错误,模型是user.rb,我无法弄清楚发生了什么。
All of the advice I have found我找到的所有建议
So, if anyone is running into this problem, and you have the files you need, check to see if you're loading the rails environment.因此,如果有人遇到此问题,并且您拥有所需的文件,请检查您是否正在加载 rails 环境。 I added:我补充说:
require "#{File.dirname(__FILE__)}/config/environment.rb"
to the top of the file I was running where I was calling the classes and that solved the problem.到我正在运行的文件的顶部,我正在调用类并解决了问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.