Answers:
在environment.rb
文件中取消注释此行:
config.frameworks -= [ :active_record, :active_resource, :action_mailer]
对于Rails 3和Rails 4:
使用
-O
(大写的“ O”)或--skip-activerecord
选项来生成没有数据库的应用程序。
rails new myApp -O
要么
rails new myApp --skip-activerecord
此答案从这里转发
对于Rails 5:
使用
--skip-active-record
选项来生成没有数据库的应用程序
请注意,与以前的Rails版本相比,多余的连字符“-”。
rails new myApp --skip-active-record
-O
适用于Rails4.2.x 。该选项--skip-activerecord
不起作用。
--skip-active-record
(请注意活动nad记录之间的破折号)。另一种方式对我不起作用。
对于现有的Rails 4/5/6项目,在config/application.rb
文件中包含以下行:
require 'rails/all' # or `require "rails"' in newer versions
(作为参考,该行正在加载此文件)
因此,必须加载每个库,而不是分别加载所有,如下所示:
# active_record is what we're not going to use it, so comment it "just in case"
# require "active_record/railtie"
# This is not loaded in rails/all but inside active_record so add it if
# you want your models work as expected
require "active_model/railtie"
# And now the rest
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "active_job/railtie" # Only for Rails >= 4.2
require "action_cable/engine" # Only for Rails >= 5.0
require "sprockets/railtie"
require "rails/test_unit/railtie"
# All these depend on active_record, so they should be excluded also
# require "action_text/engine" # Only for Rails >= 6.0
# require "action_mailbox/engine" # Only for Rails >= 6.0
# require "active_storage/engine" # Only for Rails >= 5.2
请密切注意评论,以了解有关Rails版本的内容。
还要检查以下文件(如果有),并注释以下几行:
# package.json
"@rails/activestorage": "^6.0.0",
# app/javascript/packs/application.js
require("@rails/activestorage").start()
# bin/setup
system! 'bin/rails db:prepare'
# config/environments/development.rb
config.active_storage.service = :local # For Rails >= 5.2
config.active_record.migration_error = :page_load
config.active_record.verbose_query_logs = true
# config/environments/test.rb
config.active_storage.service = :test # For Rails >= 5.2
# config/environments/production.rb
config.active_storage.service = :local # For Rails >= 5.2
config.active_record.dump_schema_after_migration = false
# spec/rails_helper.rb
ActiveRecord::Migration.maintain_test_schema!
# test/test_helper.rb
fixtures :all # In case you're using fixtures
# Only for Rails >= 5.0
#config/initializers/new_framework_defaults.rb
Rails.application.config.active_record.belongs_to_required_by_default = true
还要删除ActiveRecord::Base
模型文件中的所有引用(如果适用,也可以简单地删除文件)。例如,自动生成的app/models/application_record.rb
文件。
active_job/railtie
和action_cable/engine
database.yml
因此它只会忽略数据库?
rake assets:precompile
由于NoMethodError: undefined method
active_record'`:/
activerecord-nulldb-adapter
来自github.com/nulldb/nulldb的内容允许您换出适配器以进行临时的或侵入性较小的更改。
-O
与action_mailer
不相关的Rails应用程序config/detabase.yml
,因此无需对其进行评论。评论后,还有一些额外的行需要评论active_storage
。
在Rails 4中,启动新项目时,可以使用-O或--skip-active-record
rails new my_project -O
rails new my_project --skip-active-record
如果您已经创建了一个项目,则需要发表评论
require "active_record/railtie"
从config / application.rb和
config.active_record.migration_error = :page_load
来自config / environments / development.rb
如果您不需要数据库,则可能不需要大量的Rails。您可能希望使用更小,更可定制的框架。
Sinatra是一个很小的框架,非常适合提供基本的静态页面。
为了支持Rails 6 rc1和activerecord-nulldb-adapter
gem,我们需要一个猴子补丁
在config / initializers / null_db_adapter_monkey_patches.rb中
module ActiveRecord
module ConnectionAdapters
class NullDBAdapter < ActiveRecord::ConnectionAdapters::AbstractAdapter
def new_table_definition(table_name = nil, is_temporary = nil)
TableDefinition.new(table_name, is_temporary)
end
end
end
end