LoginSignup
4
1

More than 1 year has passed since last update.

[Rails]Mysql2::Error:Unknown column(フォロー機能)

Posted at

はじめに

本記事では、フォロー機能導入中に起きたエラーの
私が行った対処法を記述します。
昨日もフォロー機能を実装しており、ようやく終わりといった感じです。
前回の記事になります。

エラー内容

ActiveRecord::StatementInvalid in ~
Mysql2::Error:Unknown column 'relationships.user_id' in 'where clause'
というエラーになりました。

clause=句
という意味のようです。(Google先生)

カラムエラー(フォロー機能).jpeg

内容としては、'relationships.user_id'というカラムは知らんぜ。
と言っています。

??

コード

routes.rb
Rails.application.routes.draw do

  devise_for :users
  root to: 'foods#index'
  resources :foods do
    collection do
      get :search
    end
    resource :likes, only: [:create, :destroy]
  end
  resources :users do
    resources :relationships, only: [:create, :destroy]
    get :followings, on: :member
    get :followers, on: :member
  end
end

フォロー機能なので、Userモデル

user.rb
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  has_many :foods, dependent: :destroy
  has_many :likes, dependent: :destroy
  has_many :liked_foods, through: :likes, source: :food

  has_many :relationships
  has_many :followings, through: :relationships, source: :follower
  has_many :reverse_of_relationships, class_name: 'Relationship', foreign_key: :follower_id
  has_many :followers, through: :reverse_of_relationships, source: :following


  has_one_attached :image

  def already_liked?(food)
    self.likes.exists?(food_id: food)
  end

  def is_followed_by?(user)
    reverse_of_relationships.find_by(following_id: user.id).present?
  end

end
該当箇所のみ

<div class="mypage-follow-info">
  <h2 class="mypage-follow">フォロー</h2>
    <%= link_to @user.followings.count, followings_user_path(@user) %>
  <h2 class="mypage-follow">フォロワー</h2>
    <%= link_to @user.followers.count, followers_user_path(@user) %>
</div>

原因究明

よくわからないので、とりあえず、怒られているところを丸々消してみました。

コメントアウトしてみた
<div class="mypage-follow-info">
  <h2 class="mypage-follow">フォロー</h2>
    <%#= link_to @user.followings.count, followings_user_path(@user) %>
  <h2 class="mypage-follow">フォロワー</h2>
    <%= link_to @user.followers.count, followers_user_path(@user) %>
</div>

すると、ブラウザがコメントアウトしたところ抜いて無事に表示されました。

<%= link_to @user.followers.count, followers_user_path(@user) %>

ここは表示されたので、
原因は

<%= link_to @user.followings.count, followings_user_path(@user) %>

であることが判明。

形は、followersでは問題なかったので、
モデルかコントローラーではないかと探しました。

結果

モデルでした。

user.rb
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  has_many :foods, dependent: :destroy
  has_many :likes, dependent: :destroy
  has_many :liked_foods, through: :likes, source: :food

  has_many :relationships ←ここ!!
  has_many :followings, through: :relationships, source: :follower
  has_many :reverse_of_relationships, class_name: 'Relationship', foreign_key: :follower_id
  has_many :followers, through: :reverse_of_relationships, source: :following


  has_one_attached :image

  def already_liked?(food)
    self.likes.exists?(food_id: food)
  end

  def is_followed_by?(user)
    reverse_of_relationships.find_by(following_id: user.id).present?
  end

end
user.rb
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  has_many :foods, dependent: :destroy
  has_many :likes, dependent: :destroy
  has_many :liked_foods, through: :likes, source: :food

  has_many :relationships, foreign_key: :following_id
  has_many :followings, through: :relationships, source: :follower
  has_many :reverse_of_relationships, class_name: 'Relationship', foreign_key: :follower_id
  has_many :followers, through: :reverse_of_relationships, source: :following


  has_one_attached :image

  def already_liked?(food)
    self.likes.exists?(food_id: food)
  end

  def is_followed_by?(user)
    reverse_of_relationships.find_by(following_id: user.id).present?
  end

end
has_many :relationships

foreign_key: :following_idが記述できていませんでした。

外部キー (foreign key)
【SQL入門】外部キーとは?主キーとの関係や作成方法について解説

外部キー(FOREIGN KEY)とは、
関連したテーブル間を結ぶために設定する列のことで、データの整合性をデータベースに保証させるために利用します。

設定し忘れていたので、followingが宙に浮いていた感じですね。

終わりに

オリジナルアプリにDockerを取り扱いたいと思ったため、
Dockerの学習を始めました。
ちょっと難しいく頭が混乱しますが、
仮想という言葉が好きらしく、興味は沸いてます。
YouTubeでこの方と一緒に同じことをしていました。
よければ見てみてください。
Docker超入門講座 合併版 | ゼロから実践する4時間のフルコース

今回の参考記事です。
Mysql2::Error: Unknown column 'favorites.true' in 'where clause'の対処について

明日は日曜日ですが、
引き続きDockerの学習も頑張ります!
どこかのタイミングでDockerの記事も書きたい!

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1