Untuk proyek Rails 4/5/6 yang ada, dalam config/application.rb
file Anda, Anda memiliki baris berikut:
require 'rails/all' # or `require "rails"' in newer versions
(Sebagai referensi baris tersebut memuat file ini )
Jadi, alih-alih memuat SEMUA, Anda harus memuat setiap pustaka secara terpisah sebagai berikut:
# 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
Perhatikan komentar untuk mengetahui apa yang harus dimuat terkait versi Rails Anda.
Periksa juga file berikut (jika Anda memilikinya) dan beri komentar pada baris berikut:
# 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
Hapus juga referensi apa pun ActiveRecord::Base
di file model Anda (atau cukup hapus file jika berlaku). Misalnya, app/models/application_record.rb
file yang dibuat secara otomatis .