10
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rails newで--skip-active-recordをつけ[た|なかった]時の違い

Posted at

Mongo 6.3の公式ドキュメントには以下のように書かれています。

If you are creating a new Rails application, you’ll have to use the --skip-active-record flag with rails new.

rails newの時に--skip-active-recordを付け忘れた場合でも、どこに差異があるかを把握していれば後からでも直せるじゃん!ってことで一度実験したのでメモ的な意味で記事にしました。

この記事中でのバージョンなど

  • Ruby 2.4.1
  • Rails 5.1.4

diffるためのrailsアプリケーションを作る

  1. rails newするためのrailsを用意

    $ mkdir -p rails_app_1/sample_app
    $ cd rails_app_1/sample_app
    $ cat <<EOS >Gemfile
    source 'http://rubygems.org'
    gem 'rails'
    EOS
    $ bundle install --path tmp/bundle
    
  2. ここで複製

    $ cd ../../
    $ cp -r rails_app_1 rails_app_2
    
  3. それぞれrails newする

    $ cd rails_app_1/sample_app
    $ bundle exec rails new . --skip-bundle --force
    $ cd ../../rails_app_2/sample_app
    $ bundle exec rails new . --skip-bundle --skip-active-record --force
    

    --forceオプションは1.で作ったGemfileを上書きするためです。

diffる

$ diff -uprN rails_app_1/sample_app rails_app_2/sample_app > diff.patch

Gist: diff.patch

.gitignore

  • /db/以下を削除

    diff -uprN rails_app_1/sample_app/.gitignore rails_app_2/sample_app/.gitignore
    --- rails_app_1/sample_app/.gitignore 2018-02-03 20:14:53.000000000 +0900
    +++ rails_app_2/sample_app/.gitignore 2018-02-03 20:16:18.000000000 +0900
    @@ -7,10 +7,6 @@
     # Ignore bundler config.
     /.bundle
    
    -# Ignore the default SQLite database.
    -/db/*.sqlite3
    -/db/*.sqlite3-journal
    -
     # Ignore all logfiles and tempfiles.
     /log/*
     /tmp/*
    

Gemfile

  • SQLite3を削除

    diff -uprN rails_app_1/sample_app/Gemfile rails_app_2/sample_app/Gemfile
    --- rails_app_1/sample_app/Gemfile  2018-02-03 20:14:53.000000000 +0900
    +++ rails_app_2/sample_app/Gemfile  2018-02-03 20:16:18.000000000 +0900
    @@ -8,8 +8,6 @@ end
    
     # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
     gem 'rails', '~> 5.1.4'
    -# Use sqlite3 as the database for Active Record
    -gem 'sqlite3'
     # Use Puma as the app server
     gem 'puma', '~> 3.7'
     # Use SCSS for stylesheets
    

app/models/application_record.rb

  • ファイルごと削除

    diff -uprN rails_app_1/sample_app/app/models/application_record.rb rails_app_2/sample_app/app/models/application_record.rb
    --- rails_app_1/sample_app/app/models/application_record.rb 2018-02-03 20:14:53.000000000 +0900
    +++ rails_app_2/sample_app/app/models/application_record.rb 1970-01-01 09:00:00.000000000 +0900
    @@ -1,3 +0,0 @@
    -class ApplicationRecord < ActiveRecord::Base
    -  self.abstract_class = true
    -end
    

bin/setup

  • rails db:setupの呼び出しを削除

    diff -uprN rails_app_1/sample_app/bin/setup rails_app_2/sample_app/bin/setup
    --- rails_app_1/sample_app/bin/setup  2018-02-03 20:14:53.000000000 +0900
    +++ rails_app_2/sample_app/bin/setup  2018-02-03 20:16:18.000000000 +0900
    @@ -22,14 +22,6 @@ chdir APP_ROOT do
       # system('bin/yarn')
    
    
    -  # puts "\n== Copying sample files =="
    -  # unless File.exist?('config/database.yml')
    -  #   cp 'config/database.yml.sample', 'config/database.yml'
    -  # end
    -
    -  puts "\n== Preparing database =="
    -  system! 'bin/rails db:setup'
    -
       puts "\n== Removing old logs and tempfiles =="
       system! 'bin/rails log:clear tmp:clear'
    
    

bin/update

  • rails db:migrateの呼び出しを削除

    diff -uprN rails_app_1/sample_app/bin/update rails_app_2/sample_app/bin/update
    --- rails_app_1/sample_app/bin/update 2018-02-03 20:14:53.000000000 +0900
    +++ rails_app_2/sample_app/bin/update 2018-02-03 20:16:18.000000000 +0900
    @@ -18,9 +18,6 @@ chdir APP_ROOT do
       system! 'gem install bundler --conservative'
       system('bundle check') || system!('bundle install')
    
    -  puts "\n== Updating database =="
    -  system! 'bin/rails db:migrate'
    -
       puts "\n== Removing old logs and tempfiles =="
       system! 'bin/rails log:clear tmp:clear'
    
    

config/application.rb

  • require 'rails/all'を使わず、個別にロード

    diff -uprN rails_app_1/sample_app/config/application.rb rails_app_2/sample_app/config/application.rb
    --- rails_app_1/sample_app/config/application.rb  2018-02-03 20:14:53.000000000 +0900
    +++ rails_app_2/sample_app/config/application.rb  2018-02-03 20:16:18.000000000 +0900
    @@ -1,6 +1,16 @@
     require_relative 'boot'
    
    -require 'rails/all'
    +require "rails"
    +# Pick the frameworks you want:
    +require "active_model/railtie"
    +require "active_job/railtie"
    +# require "active_record/railtie"
    +require "action_controller/railtie"
    +require "action_mailer/railtie"
    +require "action_view/railtie"
    +require "action_cable/engine"
    +require "sprockets/railtie"
    +require "rails/test_unit/railtie"
    
     # Require the gems listed in Gemfile, including any gems
     # you've limited to :test, :development, or :production.
    

ちなみにrequire 'rails/all'をすると

vendor/bundle/ruby/2.4.0/gems/railties-5.1.2/lib/rails/all.rb
require "rails"

%w(
  active_record/railtie
  action_controller/railtie
  action_view/railtie
  action_mailer/railtie
  active_job/railtie
  action_cable/engine
  rails/test_unit/railtie
  sprockets/railtie
).each do |railtie|
  begin
    require railtie
  rescue LoadError
  end
end

以上のソースがロードされます。
--skip-active-recordをしたときは、ActiveRecordは除外するけどActiveModelはロードするという挙動のようです。
まぁMongoidもActiveModelは使いますしね。

config/database.yml

  • ファイルごと削除

    diff -uprN rails_app_1/sample_app/config/database.yml rails_app_2/sample_app/config/database.yml
    --- rails_app_1/sample_app/config/database.yml  2018-02-03 20:14:53.000000000 +0900
    +++ rails_app_2/sample_app/config/database.yml  1970-01-01 09:00:00.000000000 +0900
    @@ -1,25 +0,0 @@
    -# SQLite version 3.x
    -#   gem install sqlite3
    -#
    -#   Ensure the SQLite 3 gem is defined in your Gemfile
    -#   gem 'sqlite3'
    -#
    -default: &default
    -  adapter: sqlite3
    -  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
    -  timeout: 5000
    -
    -development:
    -  <<: *default
    -  database: db/development.sqlite3
    -
    -# Warning: The database defined as "test" will be erased and
    -# re-generated from your development database when you run "rake".
    -# Do not set this db to the same as development or production.
    -test:
    -  <<: *default
    -  database: db/test.sqlite3
    -
    -production:
    -  <<: *default
    -  database: db/production.sqlite3
    

config/environments/development.rb

  • config.active_record.migration_error = :page_load 削除

    diff -uprN rails_app_1/sample_app/config/environments/development.rb rails_app_2/sample_app/config/environments/development.rb
    --- rails_app_1/sample_app/config/environments/development.rb 2018-02-03 20:14:53.000000000 +0900
    +++ rails_app_2/sample_app/config/environments/development.rb 2018-02-03 20:16:19.000000000 +0900
    @@ -34,9 +34,6 @@ Rails.application.configure do
       # Print deprecation notices to the Rails logger.
       config.active_support.deprecation = :log
    
    -  # Raise an error on page load if there are pending migrations.
    -  config.active_record.migration_error = :page_load
    -
       # Debug mode disables concatenation and preprocessing of assets.
       # This option may cause significant delays in view rendering with a large
       # number of complex assets.
    

config/environments/production.rb

  • config.active_record.dump_schema_after_migration = false 削除

    diff -uprN rails_app_1/sample_app/config/environments/production.rb rails_app_2/sample_app/config/environments/production.rb
    --- rails_app_1/sample_app/config/environments/production.rb  2018-02-03 20:14:53.000000000 +0900
    +++ rails_app_2/sample_app/config/environments/production.rb  2018-02-03 20:16:19.000000000 +0900
    @@ -85,7 +85,4 @@ Rails.application.configure do
         logger.formatter = config.log_formatter
         config.logger    = ActiveSupport::TaggedLogging.new(logger)
       end
    -
    -  # Do not dump schema after migrations.
    -  config.active_record.dump_schema_after_migration = false
     end
    

config/initializers/wrap_parameters.rb

  • ActiveSupport.on_load(:active_record) 削除(もともとコメントだけど)

    diff -uprN rails_app_1/sample_app/config/initializers/wrap_parameters.rb rails_app_2/sample_app/config/initializers/wrap_parameters.rb
    --- rails_app_1/sample_app/config/initializers/wrap_parameters.rb 2018-02-03 20:14:53.000000000 +0900
    +++ rails_app_2/sample_app/config/initializers/wrap_parameters.rb 2018-02-03 20:16:19.000000000 +0900
    @@ -7,8 +7,3 @@
     ActiveSupport.on_load(:action_controller) do
       wrap_parameters format: [:json]
     end
    -
    -# To enable root element in JSON for ActiveRecord objects.
    -# ActiveSupport.on_load(:active_record) do
    -#   self.include_root_in_json = true
    -# end
    

db/seeds.rb

  • db/ディレクトリごと削除

    diff -uprN rails_app_1/sample_app/db/seeds.rb rails_app_2/sample_app/db/seeds.rb
    --- rails_app_1/sample_app/db/seeds.rb  2018-02-03 20:14:53.000000000 +0900
    +++ rails_app_2/sample_app/db/seeds.rb  1970-01-01 09:00:00.000000000 +0900
    @@ -1,7 +0,0 @@
    -# This file should contain all the record creation needed to seed the database with its default values.
    -# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
    -#
    -# Examples:
    -#
    -#   movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
    -#   Character.create(name: 'Luke', movie: movies.first)
    

test/test_helper.rb

  • fixtures :all 削除

    diff -uprN rails_app_1/sample_app/test/test_helper.rb rails_app_2/sample_app/test/test_helper.rb
    --- rails_app_1/sample_app/test/test_helper.rb  2018-02-03 20:14:53.000000000 +0900
    +++ rails_app_2/sample_app/test/test_helper.rb  2018-02-03 20:16:19.000000000 +0900
    @@ -2,8 +2,5 @@ require File.expand_path('../../config/e
     require 'rails/test_help'
    
     class ActiveSupport::TestCase
    -  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
    -  fixtures :all
    -
       # Add more helper methods to be used by all tests here...
     end
    

以上

10
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?