有问题的Rails应用最初是使用--skip-active-record
创建的,因为不需要数据库。
现在需要在应用程序中添加MySQL数据库。如何将非数据库Rails应用程序转换为具有数据库?
答案 0 :(得分:0)
这是我所做的:
使用新的my_app_name -d mysql
文件已更改:
config/application.rb
config/environments/development.rb
config/environments/production.rb
config/initializers/wrap_parameters.rb
Gemfile
bin/setup
bin/update
test/test_helper.rb
新文件
config/database.yml
app/models/application_record.rb
db/seeds.rb
详细信息(差异视图-已删除+已添加):
config / application.rb
-# 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 'rails/all'
config / environments / development.rb
+# Raise an error on page load if there are pending migrations.
+config.active_record.migration_error = :page_load
config / environments / production.rb
+#迁移后不转储架构。 + config.active_record.dump_schema_after_migration = false
宝石文件
+# Use mysql as the database for Active Record
+gem 'mysql2', '>= 0.3.18', '< 0.6.0'
bin /设置
+# 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'
bin / update
+puts "\n== Updating database =="
+system! 'bin/rails db:migrate'
app / models / application_record.rb
+class ApplicationRecord < ActiveRecord::Base
+ self.abstract_class = true
+end
config / database.yml
+default: &default
+ adapter: mysql2
+ encoding: utf8
+ pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
+ username: root
+ password:
+ host: localhost
+development:
+ <<: *default
+ database: app-without-activerecord_development
+test:
+ <<: *default
+ database: app-without-activerecord_test
+production:
+ <<: *default
+ database: app-without-activerecord_production
+ username: app-without-activerecord
+ password: <%= ENV['APP-WITHOUT-ACTIVERECORD_DATABASE_PASSWORD'] %>
答案 1 :(得分:0)
阅读您的问题后,我要做的第一件事是在计算机上创建2个新项目
rails new test-db
和rails new test-no-db --skip-active-record
现在我应该有2个相同的项目,但是1个没有活动记录
现在可以在两个目录diff --brief -r test-db/ test-no-db/
更改的文件
1)Gemfile
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
2)app / assets / javascripts / application.js
//= require activestorage
3)添加此文件
#test-db/app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
4)test-db / bin / setup
chdir APP_ROOT do
...
puts "\n== Preparing database =="
system! 'bin/rails db:setup'
...
end
您可以扔掉其余文件,以查看有什么不同。 我知道这不是您想要的解决方案,而是我会做的