我设计了以下迁移,并想与社区核实是否符合 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
结束
最佳答案
在这种情况下,最佳实践将规定:
- 将每个
create_table
分解为自己的迁移。 - 您的表名应该是复数形式,例如
婚姻状况
。 - 时间戳是个好主意。
- 考虑为将定期查询的字段添加索引,例如模型名称、用户电子邮件或外键。
查看有关迁移的 Rails 指南,了解有关最佳实践的信息:https://edgeguides.rubyonrails.org/active_record_migrations.html
关于ruby-on-rails - Rails - 数据库迁移最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53968978/