Rails 5.2无ActiveRecord项目生成模型报错求助
Hey there, I get why this error feels confusing—you explicitly skipped ActiveRecord when creating your project, so why is Rails still bugging you about sqlite3? Let's break this down and fix it step by step.
Why This Happens
Even though you used --skip-active-record to set up your project, Rails' default model generator is hardwired to work with ActiveRecord out of the box. When you run a command like rails generate model, it still tries to load ActiveRecord's database adapter logic... but since you don't have sqlite3 (or any database gem) in your Gemfile, it throws that frustrating Gem::LoadError.
Solutions to Fix It
1. Just Create Your Model Manually (Simplest Option)
If you don't need the generator's extra files, skip it entirely. Just make a plain Ruby class in app/models/:
# app/models/user.rb class User # Add your custom logic here (validations, methods, etc.) include ActiveModel::Validations validates :name, presence: true end
This gives you full control, and you won't trigger any ActiveRecord-related code at all.
2. Configure Rails to Fully Disable ActiveRecord
If you want to use generators without ActiveRecord interference, tweak your project's config to make sure Rails knows it shouldn't load ActiveRecord components:
- Open
config/application.rband replace the defaultrequire "rails/all"with a list of railties that exclude ActiveRecord:require "rails" # Include railties manually, skipping Active Record require "active_model/railtie" require "active_job/railtie" require "active_storage/engine" require "action_controller/railtie" require "action_mailer/railtie" require "action_view/railtie" require "action_cable/engine" require "sprockets/railtie" require "rails/test_unit/railtie" - Delete the
config/database.ymlfile (it's only for ActiveRecord, so it's useless here) - Check your environment files (in
config/environments/) and remove any lines starting withconfig.active_record—these are ActiveRecord-specific settings Rails might still be trying to load.
3. Use the Generator with the --orm none Flag
If you prefer using the generator but don't want ActiveRecord ties, add the --orm none parameter when running the model generator:
rails generate model User --orm none
This will generate a basic model file without any ActiveRecord dependencies, just like the manual option but with Rails handling the file structure for you.
Final Note
Once you make these changes, Rails will stop trying to load ActiveRecord components entirely, so that sqlite3 error should vanish for good.
内容的提问来源于stack exchange,提问作者sidneivl

