質問編集履歴

1

モデルの追記

2019/08/30 05:03

投稿

salmon_trout
salmon_trout

スコア7

test CHANGED
File without changes
test CHANGED
@@ -215,3 +215,103 @@
215
215
  <li class="nav-item"><%= link_to 'カテゴリ', categories_path, class: 'nav-link js-scroll-trigger' %></li>
216
216
 
217
217
  ```
218
+
219
+
220
+
221
+ ```
222
+
223
+ class Category < ApplicationRecord
224
+
225
+ has_many :posts
226
+
227
+ end
228
+
229
+
230
+
231
+ ```
232
+
233
+
234
+
235
+ ```
236
+
237
+ class Post < ApplicationRecord
238
+
239
+
240
+
241
+ belongs_to :user
242
+
243
+ has_many :comments
244
+
245
+ has_many :favorites, dependent: :destroy
246
+
247
+ belongs_to :category
248
+
249
+
250
+
251
+ def favorited_by?(user)
252
+
253
+ favorites.where(user_id: user.id).exists?
254
+
255
+ end
256
+
257
+ end
258
+
259
+
260
+
261
+ ```
262
+
263
+ ```
264
+
265
+ class CreateCategories < ActiveRecord::Migration[5.2]
266
+
267
+ def change
268
+
269
+ create_table :categories do |t|
270
+
271
+ t.string :symptoms
272
+
273
+
274
+
275
+ t.timestamps
276
+
277
+ end
278
+
279
+ end
280
+
281
+ end
282
+
283
+
284
+
285
+ ```
286
+
287
+
288
+
289
+ ```
290
+
291
+ class CreatePosts < ActiveRecord::Migration[5.2]
292
+
293
+ def change
294
+
295
+ create_table :posts do |t|
296
+
297
+ t.string :title
298
+
299
+ t.string :content
300
+
301
+ t.integer :user_id
302
+
303
+ t.integer :category_id
304
+
305
+
306
+
307
+ t.timestamps
308
+
309
+ end
310
+
311
+ end
312
+
313
+ end
314
+
315
+
316
+
317
+ ```