質問編集履歴
3
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,9 +2,29 @@
|
|
2
2
|
|
3
3
|
railsでSNSのようなサイトを作成しておりますが、
|
4
4
|
|
5
|
-
投稿一覧画面でユーザー名等を表示したいのですが
|
5
|
+
投稿一覧画面でユーザー名等を表示したいのですがエラーが発生してしまいます。
|
6
|
-
|
6
|
+
|
7
|
+
|
8
|
+
|
7
|
-
|
9
|
+
rails g migration AddUserIdToPostsでpostsとuserを紐付ける用にマイグレーションファイルをを作成しました。
|
10
|
+
|
11
|
+
```ここに言語を入力
|
12
|
+
|
13
|
+
class AddUserIdToPosts < ActiveRecord::Migration[6.1]
|
14
|
+
|
15
|
+
def change
|
16
|
+
|
17
|
+
add_reference :posts, :user, foreign_key: true
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
```
|
26
|
+
|
27
|
+
|
8
28
|
|
9
29
|
|
10
30
|
|
2
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
-
##実現したいこと
|
1
|
+
##前提・実現したいこと
|
2
2
|
|
3
3
|
railsでSNSのようなサイトを作成しておりますが、
|
4
4
|
|
5
5
|
投稿一覧画面でユーザー名等を表示したいのですがうまくいかないです。
|
6
6
|
|
7
|
+
belongs_to :userを付け rails cでPost.first.userを確認するとnilでした
|
8
|
+
|
7
9
|
|
8
10
|
|
9
11
|
##エラー内容
|
1
schema.rbを追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -218,6 +218,158 @@
|
|
218
218
|
|
219
219
|
```
|
220
220
|
|
221
|
+
##schema.rb
|
222
|
+
|
223
|
+
```ここに言語を入力
|
224
|
+
|
225
|
+
# This file is auto-generated from the current state of the database. Instead
|
226
|
+
|
227
|
+
# of editing this file, please use the migrations feature of Active Record to
|
228
|
+
|
229
|
+
# incrementally modify your database, and then regenerate this schema definition.
|
230
|
+
|
231
|
+
#
|
232
|
+
|
233
|
+
# This file is the source Rails uses to define your schema when running `bin/rails
|
234
|
+
|
235
|
+
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
|
236
|
+
|
237
|
+
# be faster and is potentially less error prone than running all of your
|
238
|
+
|
239
|
+
# migrations from scratch. Old migrations may fail to apply correctly if those
|
240
|
+
|
241
|
+
# migrations use external dependencies or application code.
|
242
|
+
|
243
|
+
#
|
244
|
+
|
245
|
+
# It's strongly recommended that you check this file into your version control system.
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
ActiveRecord::Schema.define(version: 2021_02_21_020851) do
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
254
|
+
|
255
|
+
t.string "name", null: false
|
256
|
+
|
257
|
+
t.string "record_type", null: false
|
258
|
+
|
259
|
+
t.bigint "record_id", null: false
|
260
|
+
|
261
|
+
t.bigint "blob_id", null: false
|
262
|
+
|
263
|
+
t.datetime "created_at", null: false
|
264
|
+
|
265
|
+
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
|
266
|
+
|
267
|
+
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
|
268
|
+
|
269
|
+
end
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
create_table "active_storage_blobs", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
274
|
+
|
275
|
+
t.string "key", null: false
|
276
|
+
|
277
|
+
t.string "filename", null: false
|
278
|
+
|
279
|
+
t.string "content_type"
|
280
|
+
|
281
|
+
t.text "metadata"
|
282
|
+
|
283
|
+
t.string "service_name", null: false
|
284
|
+
|
285
|
+
t.bigint "byte_size", null: false
|
286
|
+
|
287
|
+
t.string "checksum", null: false
|
288
|
+
|
289
|
+
t.datetime "created_at", null: false
|
290
|
+
|
291
|
+
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
|
292
|
+
|
293
|
+
end
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
create_table "active_storage_variant_records", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
298
|
+
|
299
|
+
t.bigint "blob_id", null: false
|
300
|
+
|
301
|
+
t.string "variation_digest", null: false
|
302
|
+
|
303
|
+
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
|
304
|
+
|
305
|
+
end
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
create_table "posts", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
310
|
+
|
311
|
+
t.string "thumbnail"
|
312
|
+
|
313
|
+
t.string "title"
|
314
|
+
|
315
|
+
t.integer "version"
|
316
|
+
|
317
|
+
t.integer "code"
|
318
|
+
|
319
|
+
t.text "description"
|
320
|
+
|
321
|
+
t.datetime "created_at", precision: 6, null: false
|
322
|
+
|
323
|
+
t.datetime "updated_at", precision: 6, null: false
|
324
|
+
|
325
|
+
t.bigint "user_id"
|
326
|
+
|
327
|
+
t.index ["user_id"], name: "index_posts_on_user_id"
|
328
|
+
|
329
|
+
end
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
create_table "users", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
334
|
+
|
335
|
+
t.string "email", default: "", null: false
|
336
|
+
|
337
|
+
t.string "encrypted_password", default: "", null: false
|
338
|
+
|
339
|
+
t.string "reset_password_token"
|
340
|
+
|
341
|
+
t.datetime "reset_password_sent_at"
|
342
|
+
|
343
|
+
t.datetime "remember_created_at"
|
344
|
+
|
345
|
+
t.datetime "created_at", precision: 6, null: false
|
346
|
+
|
347
|
+
t.datetime "updated_at", precision: 6, null: false
|
348
|
+
|
349
|
+
t.string "name", default: "", null: false
|
350
|
+
|
351
|
+
t.text "description"
|
352
|
+
|
353
|
+
t.index ["email"], name: "index_users_on_email", unique: true
|
354
|
+
|
355
|
+
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
|
356
|
+
|
357
|
+
end
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
|
362
|
+
|
363
|
+
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
|
364
|
+
|
365
|
+
add_foreign_key "posts", "users"
|
366
|
+
|
367
|
+
end
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
```
|
372
|
+
|
221
373
|
##views/posts/index.html.erb
|
222
374
|
|
223
375
|
```ここに言語を入力
|