質問編集履歴

1

回答に対しての追加記述

2020/11/28 08:10

投稿

shawn_709
shawn_709

スコア13

test CHANGED
File without changes
test CHANGED
@@ -119,3 +119,91 @@
119
119
 
120
120
 
121
121
  以上になります。ご助言いただけると幸いです。
122
+
123
+
124
+
125
+ ##追加(マイグレーションとモデル)
126
+
127
+ ```migration
128
+
129
+ #groups
130
+
131
+ class CreateGroups < ActiveRecord::Migration[6.0]
132
+
133
+ def change
134
+
135
+ create_table :groups do |t|
136
+
137
+ t.string :name, null: false, unique: true
138
+
139
+ t.string :content
140
+
141
+ t.references :user, foreign_key: true
142
+
143
+ t.timestamps
144
+
145
+ end
146
+
147
+ end
148
+
149
+ end
150
+
151
+
152
+
153
+ #group_users
154
+
155
+ class CreateGroupUsers < ActiveRecord::Migration[6.0]
156
+
157
+ def change
158
+
159
+ create_table :group_users do |t|
160
+
161
+ t.references :group, foreign_key: true
162
+
163
+ t.references :user, foreign_key: true
164
+
165
+ t.timestamps
166
+
167
+ end
168
+
169
+ end
170
+
171
+ end
172
+
173
+ ```
174
+
175
+
176
+
177
+ ```model
178
+
179
+ #Group
180
+
181
+ class Group < ApplicationRecord
182
+
183
+ has_many :group_users
184
+
185
+ has_many :users, through: :group_users, dependent: :destroy
186
+
187
+ has_many :tweets, dependent: :destroy
188
+
189
+ belongs_to :user
190
+
191
+
192
+
193
+ validates :name, presence: true, uniqueness: true
194
+
195
+ end
196
+
197
+
198
+
199
+ #GroupUser
200
+
201
+ class GroupUser < ApplicationRecord
202
+
203
+ belongs_to :group
204
+
205
+ belongs_to :user
206
+
207
+ end
208
+
209
+ ```