How to configure Ruby on Rails without a database?

It would be convenient to use Ruby on Rails for a small website project that does not need the current database. I know I can create an empty database in MySQL and go from there, but does anyone know a better way to run Rails without a database?

thank

+96
ruby-on-rails
May 04 '09 at 18:12
source share
7 answers

Uncomment this line in the environment.rb file:

 config.frameworks -= [ :active_record, :active_resource, :action_mailer] 
+54
May 04 '09 at 18:17
source share

For Rails 3 and Rails 4 :

Use -O (Capital 'O') or --skip-activerecord to create an application without a database.

rails new myApp -O

or

rails new myApp --skip-activerecord

This answer is placed in here.




For Rails 5 :

Use the --skip-active-record option to create an application without a database

Note the additional hyphen '-', unlike previous versions of Rails .

rails new myApp --skip-active-record

+102
Jul 05 '11 at 7:10
source share

For an existing Rails 4/5/6 project, there is the following line in your config/application.rb file:

 require 'rails/all' 

(As a link, this line loads this file )
Thus, instead of loading ALL, you should load each library separately as follows:

 # 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 "active_storage/engine" # Only for Rails >= 5.2 require "action_mailbox/engine" # Only for Rails >= 6.0 require "action_text/engine" # Only for Rails >= 6.0 require "sprockets/railtie" require "rails/test_unit/railtie" 

Follow the comments to know what to download regarding your version of Rails. Then comment also on the following lines:

 #config/environments/development.rb config.active_record.migration_error = :page_load config.active_record.verbose_query_logs = true #config/environments/production.rb config.active_record.dump_schema_after_migration = false #spec/rails_helper.rb ActiveRecord::Migration.maintain_test_schema! # Only for Rails >= 5.0 #config/initializers/new_framework_defaults.rb Rails.application.config.active_record.belongs_to_required_by_default = true 

If you wish, you can remove any reference to the ActiveRecord class.

 rm app/models/application_record.rb 
+45
Sep 26 '14 at 18:22
source share

UPDATE: see the section “ Rails 3 - How Do I Opt Out of a Database? ” For a question on how to do this with Rails 3.

+22
Mar 28 '11 at 15:30
source share

If you don't need a database, then you probably don't need the bulk of Rails. You may want a smaller, more customizable structure to work with.

Sinatra is a tiny framework that is great for serving basic static pages.

But if you insist on using Rails, here is an article that will show you how to do this, or here .

+14
May 04 '09 at 18:18
source share

In Rails 4, when you start a new project, you can use -O or --skip-active-record

 rails new my_project -O rails new my_project --skip-active-record 

If you have already created a project, you will need to comment

  require "active_record/railtie" 

from config / application.rb and

  config.active_record.migration_error = :page_load 

from config / environment / development.rb

+14
Sep 18 '13 at 4:15
source share

To support Rails 6 rc1 and activerecord-nulldb-adapter we need activerecord-nulldb-adapter

In 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 
0
Jun 28 '19 at 2:59
source share



All Articles