teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

回答に対しての追加記述

2020/11/28 08:10

投稿

shawn_709
shawn_709

スコア13

title CHANGED
File without changes
body CHANGED
@@ -58,4 +58,48 @@
58
58
  (省略)
59
59
  ```
60
60
 
61
- 以上になります。ご助言いただけると幸いです。
61
+ 以上になります。ご助言いただけると幸いです。
62
+
63
+ ##追加(マイグレーションとモデル)
64
+ ```migration
65
+ #groups
66
+ class CreateGroups < ActiveRecord::Migration[6.0]
67
+ def change
68
+ create_table :groups do |t|
69
+ t.string :name, null: false, unique: true
70
+ t.string :content
71
+ t.references :user, foreign_key: true
72
+ t.timestamps
73
+ end
74
+ end
75
+ end
76
+
77
+ #group_users
78
+ class CreateGroupUsers < ActiveRecord::Migration[6.0]
79
+ def change
80
+ create_table :group_users do |t|
81
+ t.references :group, foreign_key: true
82
+ t.references :user, foreign_key: true
83
+ t.timestamps
84
+ end
85
+ end
86
+ end
87
+ ```
88
+
89
+ ```model
90
+ #Group
91
+ class Group < ApplicationRecord
92
+ has_many :group_users
93
+ has_many :users, through: :group_users, dependent: :destroy
94
+ has_many :tweets, dependent: :destroy
95
+ belongs_to :user
96
+
97
+ validates :name, presence: true, uniqueness: true
98
+ end
99
+
100
+ #GroupUser
101
+ class GroupUser < ApplicationRecord
102
+ belongs_to :group
103
+ belongs_to :user
104
+ end
105
+ ```