前提・実現したいこと
Ruby on Railsでユーザー同士のフォロー機能を作っています。
こちらのサイト等を参照しました。
Ruby on Rails ~フォロー(友達申請)機能の実装(コードメモ)
しかし以下のエラーメッセージが発生し、試行錯誤するも先へ進めない状況です。
お見苦しい点がございましたら申し訳ありません。
お知恵を拝借頂けると幸いです。
エラーメッセージ
ActiveRecord::StatementInvalid in Users#show Showing C:/Users/NEC-PCuser/Dropbox/app/lang_land/app/views/users/_stats.html.erb where line #5 raised: SQLite3::SQLException: no such column: relationships.following_id: SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."following_id" WHERE "relationships"."follower_id" = ? <a href="<%= following_user_path(@user) %>"> <strong id="following" class="stat"> **<%= @user.followings.count %>** </strong> フォロー </a>
該当のコード(_stats.html.erb)
ruby
1<% @user ||= current_user %> 2<div class="stats"> 3 <a href="<%= following_user_path(@user) %>"> 4 <strong id="following" class="stat"> 5 **<%= @user.followings.count %>** 6 </strong> 7 フォロー 8 </a> 9 10 <a href="<%= followers_user_path(@user) %>"> 11 <strong id="followers" class="stat"> 12 <%= @user.followers.count %> 13 </strong> 14 フォロワー 15 </a> 16</div>
該当のコード(show.html.erb)
ruby
1<div class="main user-show"> 2 <div class="container"> 3 <div class="user"> 4 <img src="<%= "/user_images/#{@user.image_name}" %>"> 5 <h2><%= @user.name %></h2> 6 <p><%= @user.email %></p> 7 <% if @user.id == @current_user.id %> 8 <%= link_to("編集", "/users/#{@user.id}/edit") %> 9 <% end %> 10 </div> 11 <ul class="user-tabs"> 12 <li class="active"><%= link_to("投稿", "/users/#{@user.id}") %></li> 13 <li><%= link_to("いいね!", "/users/#{@user.id}/likes") %></li> 14 </ul> 15 <% @user.posts.each do |post| %> 16 <div class="posts-index-item"> 17 <div class="post-left"> 18 <img src="<%= "/user_images/#{post.user.image_name}" %>"> 19 </div> 20 <div class="post-right"> 21 <div class="post-user-name"> 22 <%= link_to(post.user.name, "/users/#{post.user.id}") %> 23 </div> 24 <%= link_to(post.title, "/posts/#{post.id}") %> 25 </div> 26 </div> 27 <% end %> 28 <span id="user_<%= @user.id %>" class="follow-wrapper"> 29 <%= render 'follow_form', user: @user if signed_in? %> 30 </span> 31 <h4> 32 <%= render 'users/stats' %> 33 </h4> 34 </div> 35</div>
該当のコード(user.rb)
ruby
1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 has_secure_password 7 8 validates :name, {presence: true} 9 validates :email, {presence: true, uniqueness: true} 10 11 def posts 12 return Post.where(user_id: self.id) 13 end 14 15 has_many :following_relationships, foreign_key: "follower_id", class_name: "Relationship", dependent: :destroy 16 has_many :followings, through: :following_relationships 17 18 has_many :follower_relationships, foreign_key: "following_id", class_name: "Relationship", dependent: :destroy 19 has_many :followers, through: :follower_relationships 20 21 def following?(other_user) 22 following_relationships.find_by(following_id: other_user.id) 23 end 24 25 def follow!(other_user) 26 following_relationships.create!(following_id: other_user.id) 27 end 28 29 def unfollow!(other_user) 30 following_relationships.find_by(following_id: other_user.id).destroy 31 end 32end
該当のコード(relationship.rb)
ruby
1class Relationship < ApplicationRecord 2 belongs_to :follower, class_name: "User" 3 belongs_to :following, class_name: "User" 4 validates :follower_id, presence: true 5 validates :following_id, presence: true 6end
該当のコード(migration create_relationships)
ruby
1class CreateRelationships < ActiveRecord::Migration[5.2] 2 def change 3 create_table :relationships do |t| 4 t.integer :follower_id 5 t.integer :followed_id 6 7 t.timestamps 8 end 9 add_index :relationships, :follower_id 10 add_index :relationships, :followed_id 11 add_index :relationships, [:follower_id, :followed_id], unique: true 12 end 13end
試したこと
deviseのインストールを、こちらを参考に4番の「rails g devise:install」まで行いました。
gem deviseをインストールする手順と理由
補足情報(FW/ツールのバージョンなど)
ruby 2.6.4p104
RubyGems 3.0.3
Rails 5.2.3
回答1件
あなたの回答
tips
プレビュー