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

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

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

if文とは様々なプログラミング言語で使用される制御構文の一種であり、条件によって処理の流れを制御します。

Ruby on Rails

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

Q&A

1回答

3701閲覧

wrong number of bind variables (1 for 2)

tomtom1

総合スコア168

if

if文とは様々なプログラミング言語で使用される制御構文の一種であり、条件によって処理の流れを制御します。

Ruby on Rails

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

0グッド

0クリップ

投稿2019/12/03 05:25

編集2019/12/03 06:23

###実現したいこと
if文をwhere条件に書き換えたいです。

###コード

if

1<% if @current_user.following?(post.user) || @current_user.id == post.user_id %>

自分がフォローしているユーザーのポスト or 自分のポスト というif文です。
そこで、where条件を使い、Controller内に書き換えようとしましたが、エラーでうまくいきません。

Controller

1Post.joins(:user).where("@current_user.following?(users) or users.id =?",@current_user.id)

Error

1wrong number of bind variables (1 for 2) in: @current_user.following?(users) or users.id =?

調べると、?の個数と引数の個数が合っていない見たいでした。
そこで、このように書き換えて見ましたが、別のエラーが出てしまっています。

Controller

1Post.joins(:user).where("@current_user.following?= ? or users.id =?",(users) ,@current_user.id)

Error

1undefined local variable or method `users' for #<PostsController:0x00007fa8b720cc60>

お分かりの方、ぜひ助言を宜しくお願いします。

追記

userrb

1class User < ApplicationRecord 2 has_many :posts, dependent: :delete_all 3 has_many :active_friendships, class_name:"Friendship", foreign_key: "follower_id", dependent: :destroy 4 has_many :passive_friendships, class_name:"Friendship", foreign_key: "followed_id", dependent: :destroy 5 has_many :following, through: :active_friendships, source: :followed 6 has_many :followers, through: :passive_friendships, source: :follower 7 8 def follow(user) 9 active_friendships.create(followed_id: user.id) 10 end 11 def unfollow(user) 12 active_friendships.find_by(followed_id: user.id).destroy 13 end 14 def following?(user) 15 following.include?(user) 16 end 17 def posts 18 return Post.where(user_id: self.id) 19 end 20 def create_notification_follow!(current_user) 21 temp = Notification.where(["visitor_id = ? and visited_id = ? and action = ? ",current_user.id, id, 'follow']) 22 if temp.blank? 23 notification = current_user.active_notifications.new( 24 visited_id: id, 25 action: 'follow' 26 ) 27 notification.save if notification.valid? 28 end 29 end 30end 31

postrb

1class Post < ApplicationRecord 2 belongs_to :user 3 4end 5

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

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

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

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

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

peperoncino000

2019/12/03 06:07

関連するモデルを追記してください
tomtom1

2019/12/03 06:24

追記に加えました
guest

回答1

0

contrllerに移そうというのは正しいです。
where("@current_user.following?(users) or users.id =?",@current_user.id)
railsでのwhere文の書き方はいろいろありますが、この形式の場合は””の中がそのまま SQL文としてRDBに送られます。ですので、?の数が合ったとしても検索文としては成り立ちません。

Post.joins(:user,:active_friendships). where("user_id = ? or active_friendships.user_id = ?" , @current_user.id, @current_user.id)

辺りかな。

投稿2019/12/05 00:38

winterboum

総合スコア23331

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

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

tomtom1

2019/12/06 07:59

ご回答ありがとうございます! controllerに移そうというのは正しいです。 →ありがとうございます。参考までにぜひお聞きできればと思うのですが、 現状、Controllerに移した方が、スッキリするし、Post.allで取得した後にifで表示するか否か分けると全てを取得して、速度が落ちてしまうので、Controller時点で仕分けをするという意図でしたが、この考えが正しいという意味でしょうか?または他の理由の正しさがあるのでしょうか? →お教え頂いた条件で下記のエラーが出てしまいました。 Can't join 'Post' to association named 'active_friendships'; perhaps you misspelled it? もしかしたら、active_friendshipsはUserにbelongしているが、Postには関係性がないのでエラーと出てしまったのでしょうか..> <?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問