#前提
####teratail風のポートフォリオを作成中。
#やりたいこと
###自分が回答した質問を表示したい。(以下、teratailのイメージ)
#自分の投稿した質問については取得・表示できている(以下キャプチャ)
#前提(テーブル設計)
usersテーブルとquestionsテーブル、answersテーブルを使用。questionsテーブルにはuser_id
が、answersテーブルにはquestion_id,user_id
を持っています。
create_table "answers", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.bigint "question_id" t.string "name" t.text "content" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.bigint "user_id", null: false t.string "author" t.index ["question_id"], name: "index_answers_on_question_id" t.index ["user_id"], name: "index_answers_on_user_id" end create_table "questions", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name" t.string "title" t.text "content" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.bigint "user_id", null: false t.string "author", null: false t.index ["user_id"], name: "index_questions_on_user_id" end create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name" t.string "email" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "password_digest" t.boolean "admin", default: false, null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.string "provider" t.string "uid" t.string "nickname" t.string "image" t.string "token" t.string "secret" t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end
#実装とエラー画面
Userコントローラのshowアクションで表示させます。
UserController
1 2def show 3 @user = User.find(params[:id]) 4 @questions = @user.questions.where(user_id: @user.id) 5 @answers = Answer.where(user_id: @user.id) 6 #以下で、回答(answers)した質問(questions)を取得しようとしている 7 @answered_questions = @questions.where(id: @answers.question_id) 8 @followings = @user.followings.page(params[:page]) 9 @followers = @user.followers.page(params[:page]) 10 end 11
@answered_questions = ...
にてanswersテーブルのquestion_id
をパラメータに質問(questions)を取得したいのですが、エラーになってしまいます。
###@answersの中身
debug画面(赤枠)です。
####@answersを取得した画面
####@answers.question_idを取得しようとした画面
やはりquestion_id
が取得できないでいます。
■2019/08/19追記
question_ids
とした時のエラー画面
ご指摘をよろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/18 01:47
2019/08/18 01:48
2019/08/18 06:52
2019/08/19 01:28