[Ruby] Zero database design until Secretsantas

less than 1 minute read

Database design Notes:

  • For many-to-many, a foreign key in the intermediate table
  • In case of many-to-one, foreign key

users table

|Column|Type|Options| |——|—-|——-| |nickname|string|null: false,unique: true| |first_name|string|null: false| |last_name|string|null: false| |first_name_kana|string|null: false| |last_name_kana|string|null: false| |birthday|date|null: false| |image|text|null: false| |email|string|null: false,default: “”, unique: true| |password|string|null: false,default: “”|

Association

  • has_one :address
  • has_many :posts
  • has_many :groups, through: :groups_users
  • has_many :groups_users
  • has_many :messages

address table

|Column|Type|Options| |——|—-|——-| |zipcode|integer|null: false| |prefecture_code|integer|null: false| |city|string|null: false| |district|string|null: false| |building|string| | |room|date| | |user|references| foreign_key: true|

Association

  • belongs_to :user, optional: true

posts table

|Column|Type|Options| |——|—-|——-| |description|text|null: false| |image|text|null: false|

Association

  • belongs_to :user
  • has_many :posts_categories
  • has_many :categoreis, through: :posts_categories

categories table

|Column|Type|Options| |——|—-|——-| |name|text|null: false|

Association

  • has_many :posts_categories
  • has_many :posts, through: :posts_categories

groups table

|Column|Type|Options| |——|—-|——-| |name|string|null: false| |budget|integer|null: false| |exchange_date|date|null: false|

Association

  • has_many :groups_users
  • has_many :users, through: :groups_users
  • has_many :messages

messages table

|Column|Type|Options| |——|—-|——-| |content|text|null: false| |image|text|null: false| |user|references| foreign_key: true| |group|references| foreign_key: true|

Association

  • belongs_to :user
  • belongs_to :group

posts_categories table

|Column|Type|Options| |——|—-|——-| |post|references| foreign_key: true| |category|references| foreign_key: true|

Association

  • belongs_to :post
  • belongs_to :category

groups_users table

|Column|Type|Options| |——|—-|——-| |group|references| foreign_key: true| |user|references| foreign_key: true|

Association

  • belongs_to :user
  • belongs_to :group

posts_users table

|Column|Type|Options| |——|—-|——-| |user|references| foreign_key: true| |category|references| foreign_key: true|

Association

  • belongs_to :user
  • belongs_to :group