質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

2547閲覧

【rails】ActiveRecordエラーの解決方法について

runban

総合スコア152

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/10/27 08:26

ユーザー認証機能を持ったSNSサービスアプリを制作しています。
いいね機能を実装しようとした際、「ActiveRecord::StatementInvalid in PostImages#show」というエラーが表示されたのですが、どの箇所が原因で、どのように解決すれば良いのか分かりません。
下記にエラー内容・エラーコード・エラーが発生したHYMLの3点を記載しておりますので、原因や解決策をご検討いただけますと幸いです。
ご教示のほど、何卒よろしくお願いいたします。
(私が記載していますコード以外に何か必要なものがありましたら、コメント欄にてご連絡いただけますとご返信させていただきます)

↓エラー画面です

Error

1ActiveRecord::StatementInvalid in PostImages#show 2Showing /home/ec2-user/environment/meshiterro2/app/views/post_images/show.html.erb where line #10 raised: 3 4SQLite3::SQLException: no such column: favorites.post_image_id: SELECT 1 AS one FROM "favorites" WHERE "favorites"."post_image_id" = ? AND "favorites"."user_id" = ? LIMIT ? 5Extracted source (around line #9): 6 7 8 def favorited_by?(user) 9 favorites.where(user_id: user.id).exists? 10 end 11 12end 13

↓モデルに記入した7行目のコードでエラーが発生しました

ErrorCode

1class PostImage < ApplicationRecord 2 belongs_to :user 3 attachment :image 4 has_many :post_comments, dependent: :destroy 5 has_many :favorites, dependent: :destroy 6 def favorited_by?(user) 7 favorites.where(user_id: user.id).exists? 8 end 9 10end

↓エラー原因の「favorite_by」メソッド(上から10行目です)を使用しているHTMLです。

HTML

1<div class="post-body"> 2 <%= attachment_image_tag @post_image, :image %> 3 <p>ショップ名:<%= @post_image.shop_name %></p> 4 <p>説明:<%= @post_image.caption %></p> 5 <p>ユーザーネーム:<%= @post_image.user.name %></p> 6 <p>投稿日時:<%= @post_image.created_at.strftime('%Y/%m/%d') %></p> 7 <% if @post_image.user == current_user %> 8 <%= link_to "削除", post_image_path(@post_image), method: :delete %> 9 <% end %> 10 <% if @post_image.favorited_by?(current_user) %> 11 <p> 12 <%= link_to post_image_favorites_path(@post_image), method: :delete do %> 13 ♥<%= @post_image.favorites.count %> いいね 14 <% end %> 15 </p> 16 <% else %> 17 <p> 18 <%= link_to post_image_favorites_path(@post_image), method: :post do %> 19 ♡<%= @post_image.favorites.count %> いいね 20 <% end %> 21 </p> 22 <% end %> 23</div> 24<div class="comments"> 25<p>コメント件数:<%= @post_image.post_comments.count %></p> 26<% @post_image.post_comments.each do |post_comment| %> 27 <p><%= image_tag('sample-author1.jpg') %></p> 28 <%= post_comment.user.name %> 29 <%= post_comment.created_at.strftime('%Y/%m/%d') %><%= post_comment.comment %> 30 <% if post_comment.user == current_user %> 31 <div class="comment-delete"> 32 <%= link_to "削除", post_image_post_comment_path(post_comment.post_image, post_comment), method: :delete %> 33 </div> 34 <% end %> 35<% end %> 36</div> 37<div class="new-comment"> 38<%= form_with(model:[@post_image, @post_comment], local: true) do |f| %> 39 <%= f.text_area :comment, rows:'5',placeholder: "コメントをここに" %> 40 <%= f.submit "送信する" %> 41<% end %> 42</div>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

winterboum

2020/10/27 09:20

model Favorite のschemaを載せてください
runban

2020/10/27 09:32

ご質問ありがとうございます。 下記がschemaファイルの内容です。 # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # Note that this schema.rb definition is the authoritative source for your # database schema. If you need to create the application database on another # system, you should be using db:schema:load, not running all the migrations # from scratch. The latter is a flawed and unsustainable approach (the more migrations # you'll amass, the slower it'll run and the greater likelihood for issues). # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 2020_10_27_072817) do create_table "favorites", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "post_comments", force: :cascade do |t| t.text "comment" t.integer "user_id" t.integer "post_image_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "post_images", force: :cascade do |t| t.text "shop_name" t.string "image_id" t.text "caption" t.integer "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "users", force: :cascade do |t| t.string "email", default: "", 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 "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false 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 end
guest

回答1

0

ベストアンサー

table "favorites" の中身が空です。
post_image_id と user_id を追加しましょう

投稿2020/10/27 11:06

winterboum

総合スコア23401

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

runban

2020/10/27 11:41

ありがとうございます!
runban

2020/10/27 11:55

ベストアンサー後にすみません、、 post_image_id と user_id を追加する方法をご教示いただいてもよろしいでしょうか、、
winterboum

2020/10/27 12:45 編集

rails g migration AddColumnToFavorit とでもして、できたfileに add_column :favorites, :user_id, :bigint の様に書いてください
runban

2020/10/27 14:01

ありがとうございます!!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問