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

質問編集履歴

3

モデルの追記

2020/08/31 12:31

投稿

maeshu
maeshu

スコア1

title CHANGED
File without changes
body CHANGED
@@ -128,63 +128,6 @@
128
128
  down 20200829155203 Remove name from tweets
129
129
  ```
130
130
 
131
- ### 該当すると思ったコード
132
-
133
- ```
134
- class TweetsController < ApplicationController
135
- before_action :authenticate_user!, except: [:index]
136
-
137
- def index
138
- @tweets = Tweet.all
139
- end
140
-
141
- def show
142
- @tweet = Tweet.find(params[:id])
143
- end
144
-
145
- def new
146
- @tweet = Tweet.new
147
- end
148
-
149
- def create
150
- @tweet = Tweet.new(tweet_params)
151
- @tweet.user_id = current_user.id
152
- if @tweet.save
153
- redirect_to tweet_path(@tweet), notice: '投稿に成功しました'
154
- else
155
- render :new
156
- end
157
- end
158
-
159
- def edit
160
- @tweet = Tweet.find(params[:id])
161
- if @tweet.user != current_user
162
- redirect_to tweets_path, alert: '不正なアクセスです'
163
- end
164
- end
165
-
166
- def update
167
- @tweet = Tweet.find(params[:id])
168
- if @tweet.update(tweet_params)
169
- redirect_to tweet_path(@tweet), notice: '更新に成功しました'
170
- else
171
- render :edit
172
- end
173
- end
174
-
175
- def destroy
176
- tweet = Tweet.find(params[:id])
177
- tweet.destroy
178
- redirect_back(fallback_location: root_path)
179
- end
180
-
181
- private
182
- def tweet_params
183
- params.require(:tweet).permit(:title, :body)
184
- end
185
-
186
- end
187
- ```
188
131
  ### 追記
189
132
 
190
133
  ```
@@ -257,4 +200,40 @@
257
200
 
258
201
  end
259
202
  ```
203
+ ```
204
+ class Tweet < ApplicationRecord
205
+ belongs_to :user
206
+ attachment :image
207
+ has_many :favorites, dependent: :destroy
208
+
209
+ with_options presence: true do
210
+ validates :title
260
- よろしくお願いします。
211
+ validates :body
212
+ end
213
+ end
214
+ ```
215
+ ```
216
+ class User < ApplicationRecord
217
+ # Include default devise modules. Others available are:
218
+ # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
219
+ devise :database_authenticatable, :registerable,
220
+ :recoverable, :rememberable, :validatable
221
+
222
+ validates :username, presence: true, uniqueness: true
223
+ attachment :profile_image
224
+ has_many :tweets, dependent: :destroy
225
+ has_many :favorites, dependent: :destroy
226
+
227
+ def already_favorited?(tweet)
228
+ self.favorites.exists?(tweet_id: tweet.id)
229
+ end
230
+ end
231
+ ```
232
+ ```
233
+ class Favorite < ApplicationRecord
234
+ belongs_to :user
235
+ belongs_to :tweet
236
+
237
+ validates_uniqueness_of :tweet_id, scope: :user_id
238
+ end
239
+ ```

2

必要ない箇所の削除

2020/08/31 12:31

投稿

maeshu
maeshu

スコア1

title CHANGED
File without changes
body CHANGED
@@ -129,28 +129,8 @@
129
129
  ```
130
130
 
131
131
  ### 該当すると思ったコード
132
- ```
133
- class CreateTweets < ActiveRecord::Migration[6.0]
134
- def change
135
- create_table :tweets do |t|
136
- t.integer :user_id
137
- t.string :title
138
- t.text :body
139
132
 
140
- t.timestamps
141
- end
142
- end
143
- end
144
133
  ```
145
-
146
- ```
147
- class RemoveNameFromTweets < ActiveRecord::Migration[6.0]
148
- def change
149
- remove_column :tweets, :image_id, :string
150
- end
151
- end
152
- ```
153
- ```
154
134
  class TweetsController < ApplicationController
155
135
  before_action :authenticate_user!, except: [:index]
156
136
 

1

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

2020/08/31 11:36

投稿

maeshu
maeshu

スコア1

title CHANGED
File without changes
body CHANGED
@@ -8,10 +8,6 @@
8
8
  ###発生したエラー・ターミナル
9
9
  投稿を削除しようとした時
10
10
  ```
11
- 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
12
- 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
13
- 2020-08-29T16:07:23.610865+00:00 app[web.1]: [d54d13b6-d44c-45d0-977e-874c9fc672ca] (2.4ms) BEGIN
14
- 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
15
11
  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
16
12
  2020-08-29T16:07:23.623514+00:00 app[web.1]: [d54d13b6-d44c-45d0-977e-874c9fc672ca] (4.5ms) ROLLBACK
17
13
  2020-08-29T16:07:23.629723+00:00 app[web.1]: [d54d13b6-d44c-45d0-977e-874c9fc672ca] Completed 500 Internal Server Error in 33ms (ActiveRecord: 20.7ms | Allocations: 2836)
@@ -119,11 +115,6 @@
119
115
  (See full trace by running task with --trace)
120
116
  ```
121
117
 
122
- ローカル環境エラー発生
123
- ```
124
- ActiveRecord::PendingMigrationError
125
- Migrations are pending. To resolve this issue, run: rails db:migrate RAILS_ENV=development
126
- ```
127
118
  rails db:migrate:status
128
119
 
129
120
  ```
@@ -214,5 +205,76 @@
214
205
 
215
206
  end
216
207
  ```
208
+ ### 追記
209
+
210
+ ```
211
+ create_table :users do |t|
212
+ ## Database authenticatable
213
+ t.string :username, null: false
214
+ t.text :profile
215
+ t.string :profile_image_id
216
+ t.string :email, null: false, default: ""
217
+ t.string :encrypted_password, null: false, default: ""
218
+ ```
219
+ ```
220
+ class CreateTweets < ActiveRecord::Migration[6.0]
221
+ def change
222
+ create_table :tweets do |t|
223
+ t.integer :user_id
224
+ t.string :title
225
+ t.text :body
226
+
227
+ t.timestamps
228
+ end
229
+ end
230
+ end
231
+ ```
232
+ ```
233
+ class CreateFavorites < ActiveRecord::Migration[6.0]
234
+ def change
235
+ create_table :favorites do |t|
236
+ t.integer :user_id
237
+ t.integer :tweet_id
238
+
239
+ t.timestamps
240
+ end
241
+ end
242
+ end
243
+ ```
244
+ ```
245
+ class RemoveNameFromTweets < ActiveRecord::Migration[6.0]
246
+ def change
217
- 初めてのherokuにデプロイとマイグレーション系のエラーでとても焦っています...。
247
+ remove_column :tweets, :image_id, :string
248
+ end
249
+ end
250
+ ```
251
+ ```
252
+ ActiveRecord::Schema.define(version: 2020_08_23_132550) do
253
+
254
+ create_table "tweets", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
255
+ t.integer "user_id"
256
+ t.string "title"
257
+ t.text "body"
258
+ t.datetime "created_at", precision: 6, null: false
259
+ t.datetime "updated_at", precision: 6, null: false
260
+ end
261
+
262
+ create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
263
+ t.string "username", null: false
264
+ t.text "profile"
265
+ t.string "profile_image_id"
266
+ t.string "email", default: "", null: false
267
+ t.string "encrypted_password", default: "", null: false
268
+ t.string "reset_password_token"
269
+ t.datetime "reset_password_sent_at"
270
+ t.datetime "remember_created_at"
271
+ t.datetime "created_at", precision: 6, null: false
272
+ t.datetime "updated_at", precision: 6, null: false
273
+ t.index ["email"], name: "index_users_on_email", unique: true
274
+ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
275
+ t.index ["username"], name: "index_users_on_username", unique: true
276
+ end
277
+
278
+ end
279
+ ```
218
- 足りない箇所はすぐに添付いたします。よろしくお願いします。
280
+ よろしくお願いします。