How to configure Ruby on Rails with no database?

Ruby on-Rails

Ruby on-Rails Problem Overview


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?

Thanks

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

For Rails 3 and Rails 4:

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

rails new myApp -O

or

rails new myApp --skip-activerecord

This Answer is reshared from here


For Rails 5:

> Use --skip-active-record option to generate an application without a database

Notice the extra hyphen '-' as opposed to previous Rails versions.

rails new myApp --skip-active-record

Solution 2 - Ruby on-Rails

For an existing Rails 4-7 project, in your config/application.rb file you have the following line:

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:

# 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" # Deprecated for Rails >= 7, so add it only if you're using it
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:

# 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::Base in your model files (or simply delete the files if apply). For example, the autogenerated app/models/application_record.rb file.

Solution 3 - Ruby on-Rails

Uncomment this line in the environment.rb file:

config.frameworks -= [ :active_record, :active_resource, :action_mailer]

Solution 4 - Ruby on-Rails

In Rails 4 when starting 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've 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/environments/development.rb

Solution 5 - Ruby on-Rails

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.

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

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

Solution 6 - Ruby on-Rails

For support Rails 6 rc1 and activerecord-nulldb-adaptergem we need a monkey patching

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobbieCanuckView Question on Stackoverflow
Solution 1 - Ruby on-RailsRSKView Answer on Stackoverflow
Solution 2 - Ruby on-RailsAlter LagosView Answer on Stackoverflow
Solution 3 - Ruby on-RailstypemismatchView Answer on Stackoverflow
Solution 4 - Ruby on-RailsJohn BarelaView Answer on Stackoverflow
Solution 5 - Ruby on-Railsvrish88View Answer on Stackoverflow
Solution 6 - Ruby on-RailsS.ChubView Answer on Stackoverflow