gpt4 book ai didi

ruby-on-rails - 如何将事件记录数据库添加回使用 --skip-active-record 创建的 Rails 5

转载 作者:行者123 更新时间:2023-12-04 03:34:13 25 4
gpt4 key购买 nike

有问题的 Rails 应用最初是使用 --skip-active-record 创建的,因为不需要数据库。

现在需要在应用程序中添加一个 MySQL 数据库。如何将非数据库 Rails 应用程序转换为具有数据库?

最佳答案

这就是我所做的:

  1. 转到文件夹应用程序的上一级并再次运行 rails new 命令,每次都按 Enter 键以覆盖冲突

rails new my_app_name -d mysql

  1. 使用 git 工具查看应用文件中的更改(RubyMine、gitg、smartgit)

文件更改:

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

详细信息(差异 View - 删除+添加):

配置/应用程序.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'

配置/环境/开发.rb

+# Raise an error on page load if there are pending migrations.
+config.active_record.migration_error = :page_load

配置/环境/production.rb

+# 迁移后不要转储架构。+config.active_record.dump_schema_after_migration = false

gem 文件

+# 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'

垃圾箱/更新

+puts "\n== Updating database =="
+system! 'bin/rails db:migrate'

应用程序/模型/application_record.rb

+class ApplicationRecord < ActiveRecord::Base
+ self.abstract_class = true
+end

配置/数据库.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'] %>

关于ruby-on-rails - 如何将事件记录数据库添加回使用 --skip-active-record 创建的 Rails 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54203162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com