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

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

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

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

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

RubyGems

RubyGemsはRubyによるプログラミングのためのパッケージマネジメントツールです。ユーザはこれを使用することで、Rubyライブラリのダウンロードやアップデートや、依存関係の自動解決が可能になります。

Ruby on Rails

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

Q&A

解決済

1回答

1713閲覧

Rails deviseを用いたフォロー機能実装時におけるエラー

punchan36

総合スコア105

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

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

RubyGems

RubyGemsはRubyによるプログラミングのためのパッケージマネジメントツールです。ユーザはこれを使用することで、Rubyライブラリのダウンロードやアップデートや、依存関係の自動解決が可能になります。

Ruby on Rails

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

0グッド

0クリップ

投稿2020/01/15 09:03

編集2020/01/15 09:39

前提・実現したいこと

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 &nbsp; 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

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

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

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

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

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

winterboum

2020/01/15 09:16

class User、Relationship,Following の関連定義の部分をを載せてください。 belongs_to, has_many などのところです 及び、それらのmigrationも
punchan36

2020/01/15 09:41

class User、class Relationship、及びmigration(create_relationships)を追加致しました。宜しくお願い致します。
guest

回答1

0

ベストアンサー

following_id であったり、followed_id であったり揺らいでますね。
統一しましょう
belongs_to :following, も合わせてください。

投稿2020/01/15 09:50

編集2020/01/15 09:50
winterboum

総合スコア23329

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

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

punchan36

2020/01/15 12:37

初歩的な部分を見落としておりました。 ご回答有難うございました!エラーもなく無事に表示されました。 ただ今度はフォローボタンが表示されない問題が出てきました。 もう少し自分で考えてみて、もし難しい様なら再度質問させて頂きます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問