【问题标题】:NameError: uninitialized constant ApplicationRecord despite having the application_record file correctlyNameError:尽管有正确的 application_record 文件,但未初始化常量 ApplicationRecord
【发布时间】:2021-09-24 19:00:03
【问题描述】:

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

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。

我的应用程序.rb:

class Application < Rails::Application

    config.load_defaults 6.1

    config.generators.system_tests = nil
  end

我使用的是 Rails 6.1。

我的宝石文件:

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,我不知道是怎么回事。

我找到的所有建议

【问题讨论】:

  • 被切断了——我发现的所有建议都只是告诉我添加我已经拥有的文件。
  • 假设您的user.rb 包含class User &lt; ApplicationRecord。如果有明显的事情,您是否也想分享一下?在模块定义之前,您的config/application.rb 中是否还有通常的require_relative "boot"require "rails" Bundler.require(*Rails.groups) 等?
  • 是的,我写的一切都正确,我回来说我想通了。我正在运行一个使用 STDIN 将 .txt 文件作为输入的任务,虽然我记得需要模型,但我完全忘记了实际加载 rails 环境。这解决了它:require "#{File.dirname(__FILE__)}/config/environment.rb"

标签: ruby-on-rails ruby activerecord rubygems ruby-on-rails-6


【解决方案1】:

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

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

到我正在运行的文件的顶部我调用类的地方,这解决了问题。

【讨论】:

  • 那么您是否使用 ruby​​ 直接从命令行运行文件? rails 服务器或您的测试套件不会自动加载环境似乎很奇怪。只是为任何未来的读者确定上下文。
猜你喜欢
  • 2019-09-15
  • 1970-01-01
  • 2016-11-14
  • 2019-11-11
  • 2017-05-14
  • 1970-01-01
  • 2015-01-19
  • 2015-10-29
  • 1970-01-01
相关资源
最近更新 更多