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

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

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

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

Q&A

解決済

1回答

773閲覧

いいね機能のところで、Mysql2::Error: Unknown column 'likes.movie_id' in 'where clause'となってしまう

kawasaki4563

総合スコア32

Ruby on Rails 6

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

0グッド

0クリップ

投稿2021/03/30 07:50

Active Storageを用いて動画と写真が投稿できるサイトを作ろうとしているのですが、いいね機能のところでエラーになってしまいます。
エラー分は以下の桃李です。

ActiveRecord::StatementInvalid in Movies#index Mysql2::Error: Unknown column 'likes.movie_id' in 'where clause'

#問題となっているソースコード
動画投稿に関するモデル

class Movie < ApplicationRecord belongs_to :user has_one_attached :movie has_many :likes end

いいね機能に関するモデル

class Like < ApplicationRecord belongs_to :user belongs_to :post belongs_to :movie end

ユーザーに関するモデル

class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_one_attached :image has_many :posts has_many :likes has_many :comments, dependent: :destroy has_many :movies def liked_by?(post_id) likes.where(post_id: post_id).exists? end def liked_by?(movie_id) likes.where(movie_id: movie.id).exists? end with_options presence: true do validates :nickname validates :mania_histry validates :enjoy_point validates :email validates :password, length: { minimum: 6 } end end

今回の動画投稿に関するコントローラーです

class MoviesController < ApplicationController before_action :authenticate_user!, only: [:new, :update, :create, :edit, :update, :destroy] before_action :find_movie, only: [:edit, :update, :show, :destroy] def index @movies= Movie.all.order(id: "DESC") @like = Like.new end def new @movie = Movie.new end def show @comment = Comment.new @comments = @movie.comments.order(id: "DESC") end def create @movie = current_user @movie = Movie.create(movie_params) if @movie.save redirect_to movies_path,notice:'投稿に成功しました' else redirect_to new_movie_path,notice:'投稿に失敗しました' end end def edit end def update @movie.update(movie_params) redirect_to movies_path end def destroy if @movie.destroy redirect_to movies_path,alert: '投稿を削除しました' else redirect_to movies_path end end private def movie_params params.require(:movie).permit(:title, :introduction, :movie).merge(user_id: current_user.id) end def find_movie @movie = Movie.find(params[:id]) end def force_redirect_unless_my_movie return redirect_to movies_path,alert:'権限がありません'if @movie.user != current_user end end

動画投稿に関するビューです(一覧ページ)

<div class="content-wrapper"> <div class="content-block"> <% @movies.each do |movie| %> <div class="content"> <div class="user-about"> <div class="image"> <% if movie.user.image.attached? %> <%= image_tag movie.user.image %> <% else %> <%= image_tag no.user.png %> <% end %> </div> <div class="profile"> <div class="name-history"> <div class="name"> <%= movie.user.nickname %> </div> <div class="mania-histry"> <%= "学習歴:#{movie.user.mania_histry}年" %> </div> </div> <div class="enjoy-point"> <%= "楽しいポイント#{movie.user.enjoy_point}"%> </div> </div> </div> <div class="text"> <p><%= movie.title %></p> <br> <p><%= movie.introduction %></p> <video src=<%= rails_blob_path(movie.movie) %> type="movie/mov", controls></video> </div> <div class="action-menu"> <% if user_signed_in? %> <div class="like"> <h3>いいね件数: <%= movie.likes.count %></h3> </div> <% else %> <div class="like"> <h3>いいね件数: <%= movie.likes.count %></h3> </div> <% end %> <% if user_signed_in? %> <div class = 'like-button'> <% if current_user.liked_by?(movie.id) %> <td><%= link_to 'いいねを外す', destroy_like_path(movie), class: "like-link", method: :DELETE %></td> <i class="fa fa-heart unlike-btn"></i> <% else %> <td><%= link_to 'いいね', create_like_path(movie), class: "like-link", method: :create %></td> <i class="fa fa-heart like-btn"></i> <% end %> </div> <% end %> <div class="comment"> <%if user_signed_in?%> <h3>コメント件数: <%= post.comments.count %></h3> <%= link_to "コメントする", "/movies/#{movie.id}", class: "comment-buttom" %> <% else %> <h3>コメント件数: <%= post.comments.count %></h3> <%= link_to "コメントを見る", "/movie/#{movie.id}", class: "comment-buttom" %> <% end %> </div> <%if user_signed_in?%> <% if current_user.id == post.user.id || user_signed_in %> <%= link_to "編集", edit_movie_path(post) %> <%= link_to "削除", movie_path(movie), method: :delete %> <% end %> <% end %> </div> </div> <% end %> </div> <div class="sidebar"> <div class="box"> </div> <div class="box"> </div> </div> </div>

動画投稿に関するビューです(投稿)

<%= form_with model: @movie, local: true do |f| %> <p>title</p><%= f.text_field :title %> <br> <p>introduction</p><%= f.text_area :introduction %> <br> <p>movie</p><%= f.file_field :movie %> <br> <%= f.submit '投稿する', class:"button"%> <% end %>

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

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

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

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

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

guest

回答1

0

自己解決

アソシエーションの問題でした。
なので、
いいね機能に関するモデルに
belongs_to :movie
を追加ました
それに加えて動画投稿に関するmodelに

has_one_attached :movie has_many :likes has_many :liked_users, through: :likes, source: :user has_many :comments, dependent: :destroy

を追記しました。
そして、ユーザーに関するモデルに

def liked_by?(movie_id) likes.where(movie_id: movie_id).exists? end

を加えた結果解決しました。

投稿2021/03/31 05:41

kawasaki4563

総合スコア32

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問