質問編集履歴

3

relationship経由の説明の追記

2021/06/02 14:58

投稿

teriyakburger
teriyakburger

スコア1

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  Ruby on Railsで、ねこ特化型のインスタグラムのようなアプリを作っています。
6
6
 
7
- User -> Cat <- cat_photos経由 -> Photo
7
+ User <- relationships経由 -> Cat <- cat_photos経由 -> Photo
8
8
 
9
9
  というアソシエーションを組んでいます。
10
10
 

2

user-cat中間テーブルrelationshipの追記

2021/06/02 14:58

投稿

teriyakburger
teriyakburger

スコア1

test CHANGED
File without changes
test CHANGED
@@ -202,7 +202,15 @@
202
202
 
203
203
  has_many :photos, dependent: :destroy
204
204
 
205
+
206
+
207
+ # フォロー関連
208
+
209
+ has_many :relationships
210
+
211
+ has_many :following, through: :relationships, source: :cat
212
+
205
- 以下省略
213
+ end
206
214
 
207
215
  ```
208
216
 
@@ -270,6 +278,30 @@
270
278
 
271
279
  ```Ruby
272
280
 
281
+ //フォロー用User_Cat中間モデル
282
+
283
+ class Relationship < ApplicationRecord
284
+
285
+ belongs_to :user
286
+
287
+ belongs_to :cat
288
+
289
+
290
+
291
+ with_options presence: true do
292
+
293
+ validates :user_id, :cat_id
294
+
295
+ end
296
+
297
+ end
298
+
299
+ ```
300
+
301
+
302
+
303
+ ```Ruby
304
+
273
305
  //Catモデルマイグレーション
274
306
 
275
307
 
@@ -380,6 +412,40 @@
380
412
 
381
413
 
382
414
 
415
+ ```Ruby
416
+
417
+ User-Catフォロー関係用テーブルマイグレーション
418
+
419
+
420
+
421
+ class CreateRelationships < ActiveRecord::Migration[6.0]
422
+
423
+ def change
424
+
425
+ create_table :relationships do |t|
426
+
427
+ t.references :user, foreign_key: true
428
+
429
+ t.references :cat, foreign_key: true
430
+
431
+
432
+
433
+ t.timestamps
434
+
435
+
436
+
437
+ t.index %i[user_id cat_id], unique: true
438
+
439
+ end
440
+
441
+ end
442
+
443
+ end
444
+
445
+ ```
446
+
447
+
448
+
383
449
 
384
450
 
385
451
  ### 試したこと

1

マイグレーションファイル・user modelを追記しました。

2021/06/02 14:56

投稿

teriyakburger
teriyakburger

スコア1

test CHANGED
File without changes
test CHANGED
@@ -184,6 +184,32 @@
184
184
 
185
185
  ```Ruby
186
186
 
187
+ //Userモデル
188
+
189
+ class User < ApplicationRecord
190
+
191
+ # Include default devise modules. Others available are:
192
+
193
+ # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
194
+
195
+ devise :database_authenticatable, :registerable,
196
+
197
+ :recoverable, :rememberable, :validatable
198
+
199
+
200
+
201
+ has_many :cats, dependent: :destroy
202
+
203
+ has_many :photos, dependent: :destroy
204
+
205
+ 以下省略
206
+
207
+ ```
208
+
209
+
210
+
211
+ ```Ruby
212
+
187
213
 
188
214
 
189
215
  //Catモデル
@@ -242,6 +268,120 @@
242
268
 
243
269
 
244
270
 
271
+ ```Ruby
272
+
273
+ //Catモデルマイグレーション
274
+
275
+
276
+
277
+ class CreateCats < ActiveRecord::Migration[6.0]
278
+
279
+ def change
280
+
281
+ create_table :cats do |t|
282
+
283
+ t.string :cat_name, null: false
284
+
285
+ t.integer :cat_sex_id
286
+
287
+ t.integer :cat_age
288
+
289
+ t.integer :cat_breed_id
290
+
291
+ t.timestamps
292
+
293
+ end
294
+
295
+ end
296
+
297
+ end
298
+
299
+
300
+
301
+ //追記
302
+
303
+ class AddUserIdToCats < ActiveRecord::Migration[6.0]
304
+
305
+ def change
306
+
307
+ add_reference :cats, :user, null: false, foreign_key: true
308
+
309
+ end
310
+
311
+ end
312
+
313
+
314
+
315
+ ```
316
+
317
+ ```Ruby
318
+
319
+ //Photoモデルマイグレーション
320
+
321
+ class CreatePhotos < ActiveRecord::Migration[6.0]
322
+
323
+ def change
324
+
325
+ create_table :photos do |t|
326
+
327
+ t.text :detail
328
+
329
+ t.timestamps
330
+
331
+ end
332
+
333
+ end
334
+
335
+ end
336
+
337
+ //追記
338
+
339
+ class AddReferencesToPhotos < ActiveRecord::Migration[6.0]
340
+
341
+ def change
342
+
343
+ add_reference :photos, :user, null: false, foreign_key: true
344
+
345
+ end
346
+
347
+ end
348
+
349
+
350
+
351
+ ```
352
+
353
+
354
+
355
+ ```Ruby
356
+
357
+ Cat-Photo中間テーブルマイグレーション
358
+
359
+
360
+
361
+ class CreateCatPhotos < ActiveRecord::Migration[6.0]
362
+
363
+ def change
364
+
365
+ create_table :cat_photos do |t|
366
+
367
+ t.references :cat, null: false, foreign_key: true
368
+
369
+ t.references :photo, null: false, foreign_key: true
370
+
371
+ t.timestamps
372
+
373
+ end
374
+
375
+ end
376
+
377
+ end
378
+
379
+ ```
380
+
381
+
382
+
383
+
384
+
245
385
  ### 試したこと
246
386
 
247
387