2017-04-07 11 views
0

で、次のとそれが正常に動作しています。この記事https://www.railstutorial.org/book/following_users続くと私はフォローに取り組んでいる状況

の助けを借りて、機能性を以下のが、私は以下の要求が受け入れられた提示ブールとして名前状況で余分な列を追加しました。 current_user.followingはcurrent_userのすべての関係を返しますが、関係の状態が真であるユーザーが必要です。 私のコードは、記事と全く同じですが、私は関係テーブルにステータスカラムを追加しました。 だから親切に私は

答えて

0

私は元のコードを知らない助けるが、直感的にこれは

current_user.following.where(status: true) 
0

https://www.railstutorial.org/book/following_usersのように動作するはずです:

class User < ApplicationRecord 
    has_many :microposts, dependent: :destroy 
    has_many :active_relationships, class_name: "Relationship", 
            foreign_key: "follower_id", 
            dependent: :destroy 
    has_many :following, through: :active_relationships, source: :followed 
    . 
    . 
    . 
end 

だから、私はあなたが以下のように行うことができると思いますあなたの問題を解決してください:

class User < ApplicationRecord 
    has_many :microposts, dependent: :destroy 
    has_many :active_relationships, ->{ where(status: true) }, 
            class_name: "Relationship", 
            foreign_key: "follower_id", 
            dependent: :destroy 
    has_many :following, through: :active_relationships, source: :followed 
    . 
    . 
    . 
end 

希望します。

+0

は、trueとfalseのステータスを持つcurrent_userのすべてのリレーションシップを返します。 – User101

+0

これは私の悪いことでした..今、感謝しています。 – User101

+0

あなたは歓迎です:) – hoangdd

関連する問題