質問編集履歴

3

モデルの追記

2020/08/31 12:31

投稿

maeshu
maeshu

スコア1

test CHANGED
File without changes
test CHANGED
@@ -258,198 +258,84 @@
258
258
 
259
259
 
260
260
 
261
- ### 該当すると思ったコード
261
+ ### 追記
262
-
263
-
264
-
262
+
263
+
264
+
265
- ```
265
+ ```
266
+
266
-
267
+ create_table :users do |t|
268
+
269
+ ## Database authenticatable
270
+
271
+ t.string :username, null: false
272
+
273
+ t.text :profile
274
+
275
+ t.string :profile_image_id
276
+
277
+ t.string :email, null: false, default: ""
278
+
279
+ t.string :encrypted_password, null: false, default: ""
280
+
281
+ ```
282
+
283
+ ```
284
+
267
- class TweetsController < ApplicationController
285
+ class CreateTweets < ActiveRecord::Migration[6.0]
268
-
269
- before_action :authenticate_user!, except: [:index]
286
+
270
-
271
-
272
-
273
- def index
274
-
275
- @tweets = Tweet.all
276
-
277
- end
278
-
279
-
280
-
281
- def show
282
-
283
- @tweet = Tweet.find(params[:id])
284
-
285
- end
286
-
287
-
288
-
289
- def new
290
-
291
- @tweet = Tweet.new
292
-
293
- end
294
-
295
-
296
-
297
- def create
287
+ def change
298
-
288
+
299
- @tweet = Tweet.new(tweet_params)
289
+ create_table :tweets do |t|
300
-
301
- @tweet.user_id = current_user.id
290
+
302
-
303
- if @tweet.save
304
-
305
- redirect_to tweet_path(@tweet), notice: '投稿に成功しました'
306
-
307
- else
308
-
309
- render :new
291
+ t.integer :user_id
292
+
293
+ t.string :title
294
+
295
+ t.text :body
296
+
297
+
298
+
299
+ t.timestamps
310
300
 
311
301
  end
312
302
 
313
303
  end
314
304
 
315
-
305
+ end
306
+
316
-
307
+ ```
308
+
309
+ ```
310
+
311
+ class CreateFavorites < ActiveRecord::Migration[6.0]
312
+
317
- def edit
313
+ def change
318
-
319
- @tweet = Tweet.find(params[:id])
314
+
320
-
321
- if @tweet.user != current_user
315
+ create_table :favorites do |t|
316
+
322
-
317
+ t.integer :user_id
318
+
323
- redirect_to tweets_path, alert: '不正なアクセスです'
319
+ t.integer :tweet_id
320
+
321
+
322
+
323
+ t.timestamps
324
324
 
325
325
  end
326
326
 
327
327
  end
328
328
 
329
-
330
-
331
- def update
332
-
333
- @tweet = Tweet.find(params[:id])
334
-
335
- if @tweet.update(tweet_params)
336
-
337
- redirect_to tweet_path(@tweet), notice: '更新に成功しました'
338
-
339
- else
340
-
341
- render :edit
342
-
343
- end
329
+ end
344
-
345
- end
330
+
346
-
347
-
348
-
349
- def destroy
350
-
351
- tweet = Tweet.find(params[:id])
352
-
353
- tweet.destroy
354
-
355
- redirect_back(fallback_location: root_path)
356
-
357
- end
358
-
359
-
360
-
361
- private
362
-
363
- def tweet_params
364
-
365
- params.require(:tweet).permit(:title, :body)
366
-
367
- end
368
-
369
-
370
-
371
- end
372
-
373
- ```
331
+ ```
374
-
375
- ### 追記
332
+
376
-
377
-
378
-
379
- ```
333
+ ```
380
-
381
- create_table :users do |t|
334
+
382
-
383
- ## Database authenticatable
384
-
385
- t.string :username, null: false
386
-
387
- t.text :profile
388
-
389
- t.string :profile_image_id
390
-
391
- t.string :email, null: false, default: ""
392
-
393
- t.string :encrypted_password, null: false, default: ""
394
-
395
- ```
396
-
397
- ```
398
-
399
- class CreateTweets < ActiveRecord::Migration[6.0]
335
+ class RemoveNameFromTweets < ActiveRecord::Migration[6.0]
400
336
 
401
337
  def change
402
338
 
403
- create_table :tweets do |t|
404
-
405
- t.integer :user_id
406
-
407
- t.string :title
408
-
409
- t.text :body
410
-
411
-
412
-
413
- t.timestamps
414
-
415
- end
416
-
417
- end
418
-
419
- end
420
-
421
- ```
422
-
423
- ```
424
-
425
- class CreateFavorites < ActiveRecord::Migration[6.0]
426
-
427
- def change
428
-
429
- create_table :favorites do |t|
430
-
431
- t.integer :user_id
432
-
433
- t.integer :tweet_id
434
-
435
-
436
-
437
- t.timestamps
438
-
439
- end
440
-
441
- end
442
-
443
- end
444
-
445
- ```
446
-
447
- ```
448
-
449
- class RemoveNameFromTweets < ActiveRecord::Migration[6.0]
450
-
451
- def change
452
-
453
339
  remove_column :tweets, :image_id, :string
454
340
 
455
341
  end
@@ -516,4 +402,76 @@
516
402
 
517
403
  ```
518
404
 
405
+ ```
406
+
407
+ class Tweet < ApplicationRecord
408
+
409
+ belongs_to :user
410
+
411
+ attachment :image
412
+
413
+ has_many :favorites, dependent: :destroy
414
+
415
+
416
+
417
+ with_options presence: true do
418
+
419
+ validates :title
420
+
519
- よろしくお願いします。
421
+ validates :body
422
+
423
+ end
424
+
425
+ end
426
+
427
+ ```
428
+
429
+ ```
430
+
431
+ class User < ApplicationRecord
432
+
433
+ # Include default devise modules. Others available are:
434
+
435
+ # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
436
+
437
+ devise :database_authenticatable, :registerable,
438
+
439
+ :recoverable, :rememberable, :validatable
440
+
441
+
442
+
443
+ validates :username, presence: true, uniqueness: true
444
+
445
+ attachment :profile_image
446
+
447
+ has_many :tweets, dependent: :destroy
448
+
449
+ has_many :favorites, dependent: :destroy
450
+
451
+
452
+
453
+ def already_favorited?(tweet)
454
+
455
+ self.favorites.exists?(tweet_id: tweet.id)
456
+
457
+ end
458
+
459
+ end
460
+
461
+ ```
462
+
463
+ ```
464
+
465
+ class Favorite < ApplicationRecord
466
+
467
+ belongs_to :user
468
+
469
+ belongs_to :tweet
470
+
471
+
472
+
473
+ validates_uniqueness_of :tweet_id, scope: :user_id
474
+
475
+ end
476
+
477
+ ```

2

必要ない箇所の削除

2020/08/31 12:31

投稿

maeshu
maeshu

スコア1

test CHANGED
File without changes
test CHANGED
@@ -260,6 +260,140 @@
260
260
 
261
261
  ### 該当すると思ったコード
262
262
 
263
+
264
+
265
+ ```
266
+
267
+ class TweetsController < ApplicationController
268
+
269
+ before_action :authenticate_user!, except: [:index]
270
+
271
+
272
+
273
+ def index
274
+
275
+ @tweets = Tweet.all
276
+
277
+ end
278
+
279
+
280
+
281
+ def show
282
+
283
+ @tweet = Tweet.find(params[:id])
284
+
285
+ end
286
+
287
+
288
+
289
+ def new
290
+
291
+ @tweet = Tweet.new
292
+
293
+ end
294
+
295
+
296
+
297
+ def create
298
+
299
+ @tweet = Tweet.new(tweet_params)
300
+
301
+ @tweet.user_id = current_user.id
302
+
303
+ if @tweet.save
304
+
305
+ redirect_to tweet_path(@tweet), notice: '投稿に成功しました'
306
+
307
+ else
308
+
309
+ render :new
310
+
311
+ end
312
+
313
+ end
314
+
315
+
316
+
317
+ def edit
318
+
319
+ @tweet = Tweet.find(params[:id])
320
+
321
+ if @tweet.user != current_user
322
+
323
+ redirect_to tweets_path, alert: '不正なアクセスです'
324
+
325
+ end
326
+
327
+ end
328
+
329
+
330
+
331
+ def update
332
+
333
+ @tweet = Tweet.find(params[:id])
334
+
335
+ if @tweet.update(tweet_params)
336
+
337
+ redirect_to tweet_path(@tweet), notice: '更新に成功しました'
338
+
339
+ else
340
+
341
+ render :edit
342
+
343
+ end
344
+
345
+ end
346
+
347
+
348
+
349
+ def destroy
350
+
351
+ tweet = Tweet.find(params[:id])
352
+
353
+ tweet.destroy
354
+
355
+ redirect_back(fallback_location: root_path)
356
+
357
+ end
358
+
359
+
360
+
361
+ private
362
+
363
+ def tweet_params
364
+
365
+ params.require(:tweet).permit(:title, :body)
366
+
367
+ end
368
+
369
+
370
+
371
+ end
372
+
373
+ ```
374
+
375
+ ### 追記
376
+
377
+
378
+
379
+ ```
380
+
381
+ create_table :users do |t|
382
+
383
+ ## Database authenticatable
384
+
385
+ t.string :username, null: false
386
+
387
+ t.text :profile
388
+
389
+ t.string :profile_image_id
390
+
391
+ t.string :email, null: false, default: ""
392
+
393
+ t.string :encrypted_password, null: false, default: ""
394
+
395
+ ```
396
+
263
397
  ```
264
398
 
265
399
  class CreateTweets < ActiveRecord::Migration[6.0]
@@ -286,7 +420,29 @@
286
420
 
287
421
  ```
288
422
 
289
-
423
+ ```
424
+
425
+ class CreateFavorites < ActiveRecord::Migration[6.0]
426
+
427
+ def change
428
+
429
+ create_table :favorites do |t|
430
+
431
+ t.integer :user_id
432
+
433
+ t.integer :tweet_id
434
+
435
+
436
+
437
+ t.timestamps
438
+
439
+ end
440
+
441
+ end
442
+
443
+ end
444
+
445
+ ```
290
446
 
291
447
  ```
292
448
 
@@ -304,105 +460,53 @@
304
460
 
305
461
  ```
306
462
 
307
- class TweetsController < ApplicationController
308
-
309
- before_action :authenticate_user!, except: [:index]
310
-
311
-
312
-
313
- def index
314
-
315
- @tweets = Tweet.all
316
-
317
- end
318
-
319
-
320
-
321
- def show
322
-
323
- @tweet = Tweet.find(params[:id])
324
-
325
- end
326
-
327
-
328
-
329
- def new
330
-
331
- @tweet = Tweet.new
332
-
333
- end
334
-
335
-
336
-
337
- def create
338
-
339
- @tweet = Tweet.new(tweet_params)
340
-
341
- @tweet.user_id = current_user.id
342
-
343
- if @tweet.save
344
-
345
- redirect_to tweet_path(@tweet), notice: '投稿に成功しました'
346
-
347
- else
348
-
349
- render :new
350
-
351
- end
352
-
353
- end
354
-
355
-
356
-
357
- def edit
358
-
359
- @tweet = Tweet.find(params[:id])
360
-
361
- if @tweet.user != current_user
362
-
363
- redirect_to tweets_path, alert: '不正なアクセスです'
364
-
365
- end
366
-
367
- end
368
-
369
-
370
-
371
- def update
372
-
373
- @tweet = Tweet.find(params[:id])
374
-
375
- if @tweet.update(tweet_params)
376
-
377
- redirect_to tweet_path(@tweet), notice: '更新に成功しました'
378
-
379
- else
380
-
381
- render :edit
382
-
383
- end
384
-
385
- end
386
-
387
-
388
-
389
- def destroy
390
-
391
- tweet = Tweet.find(params[:id])
392
-
393
- tweet.destroy
394
-
395
- redirect_back(fallback_location: root_path)
396
-
397
- end
398
-
399
-
400
-
401
- private
402
-
403
- def tweet_params
404
-
405
- params.require(:tweet).permit(:title, :body)
463
+ ActiveRecord::Schema.define(version: 2020_08_23_132550) do
464
+
465
+
466
+
467
+ create_table "tweets", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
468
+
469
+ t.integer "user_id"
470
+
471
+ t.string "title"
472
+
473
+ t.text "body"
474
+
475
+ t.datetime "created_at", precision: 6, null: false
476
+
477
+ t.datetime "updated_at", precision: 6, null: false
478
+
479
+ end
480
+
481
+
482
+
483
+ create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
484
+
485
+ t.string "username", null: false
486
+
487
+ t.text "profile"
488
+
489
+ t.string "profile_image_id"
490
+
491
+ t.string "email", default: "", null: false
492
+
493
+ t.string "encrypted_password", default: "", null: false
494
+
495
+ t.string "reset_password_token"
496
+
497
+ t.datetime "reset_password_sent_at"
498
+
499
+ t.datetime "remember_created_at"
500
+
501
+ t.datetime "created_at", precision: 6, null: false
502
+
503
+ t.datetime "updated_at", precision: 6, null: false
504
+
505
+ t.index ["email"], name: "index_users_on_email", unique: true
506
+
507
+ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
508
+
509
+ t.index ["username"], name: "index_users_on_username", unique: true
406
510
 
407
511
  end
408
512
 
@@ -412,148 +516,4 @@
412
516
 
413
517
  ```
414
518
 
415
- ### 追記
416
-
417
-
418
-
419
- ```
420
-
421
- create_table :users do |t|
422
-
423
- ## Database authenticatable
424
-
425
- t.string :username, null: false
426
-
427
- t.text :profile
428
-
429
- t.string :profile_image_id
430
-
431
- t.string :email, null: false, default: ""
432
-
433
- t.string :encrypted_password, null: false, default: ""
434
-
435
- ```
436
-
437
- ```
438
-
439
- class CreateTweets < ActiveRecord::Migration[6.0]
440
-
441
- def change
442
-
443
- create_table :tweets do |t|
444
-
445
- t.integer :user_id
446
-
447
- t.string :title
448
-
449
- t.text :body
450
-
451
-
452
-
453
- t.timestamps
454
-
455
- end
456
-
457
- end
458
-
459
- end
460
-
461
- ```
462
-
463
- ```
464
-
465
- class CreateFavorites < ActiveRecord::Migration[6.0]
466
-
467
- def change
468
-
469
- create_table :favorites do |t|
470
-
471
- t.integer :user_id
472
-
473
- t.integer :tweet_id
474
-
475
-
476
-
477
- t.timestamps
478
-
479
- end
480
-
481
- end
482
-
483
- end
484
-
485
- ```
486
-
487
- ```
488
-
489
- class RemoveNameFromTweets < ActiveRecord::Migration[6.0]
490
-
491
- def change
492
-
493
- remove_column :tweets, :image_id, :string
494
-
495
- end
496
-
497
- end
498
-
499
- ```
500
-
501
- ```
502
-
503
- ActiveRecord::Schema.define(version: 2020_08_23_132550) do
504
-
505
-
506
-
507
- create_table "tweets", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
508
-
509
- t.integer "user_id"
510
-
511
- t.string "title"
512
-
513
- t.text "body"
514
-
515
- t.datetime "created_at", precision: 6, null: false
516
-
517
- t.datetime "updated_at", precision: 6, null: false
518
-
519
- end
520
-
521
-
522
-
523
- create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
524
-
525
- t.string "username", null: false
526
-
527
- t.text "profile"
528
-
529
- t.string "profile_image_id"
530
-
531
- t.string "email", default: "", null: false
532
-
533
- t.string "encrypted_password", default: "", null: false
534
-
535
- t.string "reset_password_token"
536
-
537
- t.datetime "reset_password_sent_at"
538
-
539
- t.datetime "remember_created_at"
540
-
541
- t.datetime "created_at", precision: 6, null: false
542
-
543
- t.datetime "updated_at", precision: 6, null: false
544
-
545
- t.index ["email"], name: "index_users_on_email", unique: true
546
-
547
- t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
548
-
549
- t.index ["username"], name: "index_users_on_username", unique: true
550
-
551
- end
552
-
553
-
554
-
555
- end
556
-
557
- ```
558
-
559
519
  よろしくお願いします。

1

マイグレーションファイルの追加

2020/08/31 11:36

投稿

maeshu
maeshu

スコア1

test CHANGED
File without changes
test CHANGED
@@ -18,14 +18,6 @@
18
18
 
19
19
  ```
20
20
 
21
- 2020-08-29T16:07:23.603505+00:00 app[web.1]: [d54d13b6-d44c-45d0-977e-874c9fc672ca] User Load (2.7ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
22
-
23
- 2020-08-29T16:07:23.607711+00:00 app[web.1]: [d54d13b6-d44c-45d0-977e-874c9fc672ca] Tweet Load (3.7ms) SELECT `tweets`.* FROM `tweets` WHERE `tweets`.`id` = 11 LIMIT 1
24
-
25
- 2020-08-29T16:07:23.610865+00:00 app[web.1]: [d54d13b6-d44c-45d0-977e-874c9fc672ca] (2.4ms) BEGIN
26
-
27
- 2020-08-29T16:07:23.613490+00:00 app[web.1]: [d54d13b6-d44c-45d0-977e-874c9fc672ca] Favorite Load (2.5ms) SELECT `favorites`.* FROM `favorites` WHERE `favorites`.`tweet_id` = 11
28
-
29
21
  2020-08-29T16:07:23.618829+00:00 app[web.1]: [d54d13b6-d44c-45d0-977e-874c9fc672ca] Tweet Destroy (4.9ms) DELETE FROM `tweets` WHERE `tweets`.`id` = 11
30
22
 
31
23
  2020-08-29T16:07:23.623514+00:00 app[web.1]: [d54d13b6-d44c-45d0-977e-874c9fc672ca] (4.5ms) ROLLBACK
@@ -240,16 +232,6 @@
240
232
 
241
233
 
242
234
 
243
- ローカル環境エラー発生
244
-
245
- ```
246
-
247
- ActiveRecord::PendingMigrationError
248
-
249
- Migrations are pending. To resolve this issue, run: rails db:migrate RAILS_ENV=development
250
-
251
- ```
252
-
253
235
  rails db:migrate:status
254
236
 
255
237
 
@@ -430,6 +412,148 @@
430
412
 
431
413
  ```
432
414
 
415
+ ### 追記
416
+
417
+
418
+
419
+ ```
420
+
421
+ create_table :users do |t|
422
+
423
+ ## Database authenticatable
424
+
425
+ t.string :username, null: false
426
+
427
+ t.text :profile
428
+
429
+ t.string :profile_image_id
430
+
431
+ t.string :email, null: false, default: ""
432
+
433
+ t.string :encrypted_password, null: false, default: ""
434
+
435
+ ```
436
+
437
+ ```
438
+
439
+ class CreateTweets < ActiveRecord::Migration[6.0]
440
+
441
+ def change
442
+
443
+ create_table :tweets do |t|
444
+
445
+ t.integer :user_id
446
+
447
+ t.string :title
448
+
449
+ t.text :body
450
+
451
+
452
+
453
+ t.timestamps
454
+
455
+ end
456
+
457
+ end
458
+
459
+ end
460
+
461
+ ```
462
+
463
+ ```
464
+
465
+ class CreateFavorites < ActiveRecord::Migration[6.0]
466
+
467
+ def change
468
+
469
+ create_table :favorites do |t|
470
+
471
+ t.integer :user_id
472
+
473
+ t.integer :tweet_id
474
+
475
+
476
+
477
+ t.timestamps
478
+
479
+ end
480
+
481
+ end
482
+
483
+ end
484
+
485
+ ```
486
+
487
+ ```
488
+
489
+ class RemoveNameFromTweets < ActiveRecord::Migration[6.0]
490
+
491
+ def change
492
+
433
- 初めてのherokuにデプロイとマイグレーション系のエラーでとても焦っています...。
493
+ remove_column :tweets, :image_id, :string
494
+
434
-
495
+ end
496
+
497
+ end
498
+
499
+ ```
500
+
501
+ ```
502
+
503
+ ActiveRecord::Schema.define(version: 2020_08_23_132550) do
504
+
505
+
506
+
507
+ create_table "tweets", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
508
+
509
+ t.integer "user_id"
510
+
511
+ t.string "title"
512
+
513
+ t.text "body"
514
+
515
+ t.datetime "created_at", precision: 6, null: false
516
+
517
+ t.datetime "updated_at", precision: 6, null: false
518
+
519
+ end
520
+
521
+
522
+
523
+ create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
524
+
525
+ t.string "username", null: false
526
+
527
+ t.text "profile"
528
+
529
+ t.string "profile_image_id"
530
+
531
+ t.string "email", default: "", null: false
532
+
533
+ t.string "encrypted_password", default: "", null: false
534
+
535
+ t.string "reset_password_token"
536
+
537
+ t.datetime "reset_password_sent_at"
538
+
539
+ t.datetime "remember_created_at"
540
+
541
+ t.datetime "created_at", precision: 6, null: false
542
+
543
+ t.datetime "updated_at", precision: 6, null: false
544
+
545
+ t.index ["email"], name: "index_users_on_email", unique: true
546
+
547
+ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
548
+
549
+ t.index ["username"], name: "index_users_on_username", unique: true
550
+
551
+ end
552
+
553
+
554
+
555
+ end
556
+
557
+ ```
558
+
435
- 足りない箇所はすぐに添付いたします。よろしくお願いします。
559
+ よろしくお願いします。