首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NameError:尽管具有正确的application_record文件,但未初始化常量ApplicationRecord

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

Stack Overflow用户
提问于 2021-07-16 05:45:14
回答 1查看 230关注 0票数 1

我在使用我的实际模型时遇到了问题,当我尝试创建用户时,我得到了以下错误:

代码语言:javascript
复制
app/models/user.rb:1:in `<top (required)>': uninitialized constant ApplicationRecord (NameError)

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

看起来是这样的:

代码语言:javascript
复制
class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

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

我的application.rb:

代码语言:javascript
复制
class Application < Rails::Application

    config.load_defaults 6.1

    config.generators.system_tests = nil
  end

我使用的是Rails 6.1。

我的Gemfile:

代码语言:javascript
复制
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,我不知道怎么回事。

我找到的所有建议

EN

回答 1

Stack Overflow用户

发布于 2021-07-16 08:12:36

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

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68401097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档