Reputation: 121
I have designed the following migration and wanted to check with the community is that is meeting the best practices of rails migration. I am running a postgres db.
I am trying to achieve a db structure where the various status attached to a user is stored in a separate table. for instance, its marital status.
let me know if that sounds like a reasonably efficient table design. and what I could improve.
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
end
Upvotes: 1
Views: 1006
Reputation: 677
In this case, best practices would dictate:
create_table
into its own migration. marital_statuses
. Check out the rails guide on migrations for information about best practice: https://edgeguides.rubyonrails.org/active_record_migrations.html
Upvotes: 1
Reputation: 2483
Let's start with:
Is it a single migration? If so, I would start with splitting it into several migrations (one for each table).
Add timestamps for each table (t.timestamps null: false
). You will thank me later ;)
For statuses tables (martial_status
, professional_status
) create simpler tables with just name and references (no need to create column for each value). Also, you probably do not need the table for marital status, because you can use classy_enum instead.
In prequalifications
you have integer columns for relations (maritial_status
, professional_status
). Don't do that, use references
.
For boolean columns, define default values.
Upvotes: 1