如何在没有数据库的情况下配置 Ruby on Rails?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/821251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure Ruby on Rails with no database?
提问by RobbieCanuck
It would be convenient to use Ruby on Rails for a small website project that has no current need for a database. I know I could create an empty database in MySQL and go from there, but does anyone know a better way to run Rails without a database?
对于当前不需要数据库的小型网站项目,使用 Ruby on Rails 会很方便。我知道我可以在 MySQL 中创建一个空数据库并从那里开始,但是有没有人知道在没有数据库的情况下运行 Rails 的更好方法?
Thanks
谢谢
采纳答案by typemismatch
Uncomment this line in the environment.rbfile:
取消注释environment.rb文件中的这一行:
config.frameworks -= [ :active_record, :active_resource, :action_mailer]
回答by RSK
For Rails 3and Rails 4:
对于Rails 3和Rails 4:
Use
-O(Capital 'O') or--skip-activerecordoption to generate an application without a database.
使用
-O(Capital 'O') 或--skip-activerecord选项来生成没有数据库的应用程序。
rails new myApp -O
rails new myApp -O
or
或者
rails new myApp --skip-activerecord
rails new myApp --skip-activerecord
This Answer is reshared from here
这个答案是从这里转发的
For Rails 5:
对于Rails 5:
Use
--skip-active-recordoption to generate an application without a database
使用
--skip-active-record选项生成没有数据库的应用程序
Notice the extra hyphen '-' as opposed to previous Rails versions.
请注意与以前的 Rails 版本相反的额外连字符“-”。
rails new myApp --skip-active-record
rails new myApp --skip-active-record
回答by Alter Lagos
For an existing Rails 4/5/6 project, in your config/application.rbfile you have the following line:
对于现有的 Rails 4/5/6 项目,在您的config/application.rb文件中包含以下行:
require 'rails/all' # or `require "rails"' in newer versions
(As reference that line is loading this file)
So instead of load ALL, you must to load each library separately as follows:
(参考该行正在加载此文件)
因此,您必须按如下方式分别加载每个库,而不是加载 ALL:
# 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
Keep an eye to the comments to know what to load regarding your Rails version.
Also check the following files (in case you have them) and comment the following lines:
请留意评论以了解有关您的 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
Also remove any reference to ActiveRecord::Basein your model files (or simply delete the files if apply). For example, the autogenerated app/models/application_record.rbfile.
还要删除ActiveRecord::Base模型文件中的任何引用(或者如果适用,只需删除文件)。例如,自动生成的app/models/application_record.rb文件。
回答by John Barela
In Rails 4 when starting a new project you can use -O or --skip-active-record
在 Rails 4 中开始新项目时,您可以使用 -O 或 --skip-active-record
rails new my_project -O
rails new my_project --skip-active-record
If you've already created a project you will need to comment
如果您已经创建了一个项目,则需要发表评论
require "active_record/railtie"
from config/application.rb and
从 config/application.rb 和
config.active_record.migration_error = :page_load
from config/environments/development.rb
来自 config/environments/development.rb
回答by vrish88
If you don't need a database then you probably don't need to have the bulk of Rails. You may want a smaller more customizable framework to work with.
如果您不需要数据库,那么您可能不需要拥有大量的 Rails。您可能需要一个更小、更可定制的框架来使用。
Sinatrais a tiny framework that is great for serving up basic static pages.
Sinatra是一个很小的框架,非常适合提供基本的静态页面。
But if you insist on using Rails hereis an article that will show you how to do just that or here.
回答by S.Chub
For support Rails 6 rc1 and activerecord-nulldb-adaptergem we need a monkey patching
为了支持 Rails 6 rc1 和activerecord-nulldb-adaptergem,我们需要一个猴子补丁
In config/initializers/null_db_adapter_monkey_patches.rb
在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

