ruby-on-rails - Rails - 数据库迁移最佳实践

标签 ruby-on-rails postgresql ruby-on-rails-5 database-migration rails-migrations

我设计了以下迁移,并想与社区核实是否符合 Rails 迁移的最佳实践。我正在运行一个 postgres 数据库。

我正在尝试实现一个数据库结构,其中附加到用户的各种状态存储在一个单独的表中。例如,它的婚姻状况。

如果这听起来像是一个相当有效的表格设计,请告诉我。以及我可以改进的地方。

class CreatePrequalifications < ActiveRecord::Migration[5.2]
 def change
   create_table :prequalifications do |t|
    t.string :attachment
    t.text :description
    t.integer :marital_status
    t.integer :professional_status
    t.integer :collateral_status
    t.integer :income_direct
    t.integer :income_support
    t.integer :income_scolarship
    t.integer :income_other
    t.boolean :blacklist

    t.references :user, foreign_key: true

    t.timestamps
  end
end

create_table :marital_status do |t|
  t.string :single
  t.string :married
  t.string :other
  t.string :divorced
  t.string :with_dependants
  t.references :user, foreign_key: true
  t.references :prequalifications, foreign_key: true
end

create_table :professional_status do |t|
  t.string :self_employed
  t.string :employed
  t.string :student
  t.string :other
  t.text :description
  t.datetime :contract_created_at
  t.datetime :contract_terminated_at

  t.references :user, foreign_key: true
  t.references :prequalifications, foreign_key: true
end

create_table :collateral_status do |t|
  t.string :collateral_name

  t.string :collateral_income
  t.boolean :visale_collateral
  t.boolean :student_collateral
  t.boolean :bank_collateral

  t.references :user, foreign_key: true
  t.references :prequalifications, foreign_key: true
end

结束

最佳答案

在这种情况下,最佳实践将规定:

  1. 将每个 create_table 分解为自己的迁移。
  2. 您的表名应该是复数形式,例如婚姻状况
  3. 时间戳是个好主意。
  4. 考虑为将定期查询的字段添加索引,例如模型名称、用户电子邮件或外键。

查看有关迁移的 Rails 指南,了解有关最佳实践的信息:https://edgeguides.rubyonrails.org/active_record_migrations.html

关于ruby-on-rails - Rails - 数据库迁移最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53968978/

相关文章:

ruby-on-rails - 如何使用 Heroku 的调度程序发送每日报告

ruby-on-rails - Rails Grape,DRY Helpers 调用共享参数

javascript - Rails 在 javascript 调用后更新 DIV(从模型重新加载)

python - 如何合并 SQLAlchemy 和 postgresql 中的两个子查询

ruby-on-rails - RSpec 3.5 将参数传递给 shared_context

ruby-on-rails - Rails - 如何从模型创建数据库表(我没有迁移脚本)

postgresql - 将 hasura 与 Google Cloud Run 和 Google Cloud SQL 结合使用

R 连接到需要 SSL 的 postgresql

ruby-on-rails - 从 iOS 应用程序连接到 ActionCable

ruby-on-rails - rails 5 : Adding conditional custom validations