微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

NameError:尽管具有正确的 application_record 文件,但未初始化的常量 ApplicationRecord

如何解决NameError:尽管具有正确的 application_record 文件,但未初始化的常量 ApplicationRecord

我在使用实际模型时遇到问题,尝试创建用户时出现以下错误

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

我在 models/application_record.rb 中有以下文件

看起来像:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

当我将 User 类改为从 ActiveRecord::Base 继承时,我得到相同的错误,除了 ActiveRecord。

我的 application.rb:

class Application < Rails::Application

    config.load_defaults 6.1

    config.generators.system_tests = nil
  end

我使用的是 Rails 6.1。

我的 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]

发生了什么?我没有拼写错误,模型是user.rb,我想不通是怎么回事。

我找到的所有建议

解决方法

因此,如果有人遇到此问题,并且您拥有所需的文件,请检查您是否正在加载 rails 环境。我补充说:

require "#{File.dirname(__FILE__)}/config/environment.rb"

到我正在运行的文件的顶部我在那里调用类并解决了问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。