2017-04-07 103 views
0

我工作的後續跟蹤和本條https://www.railstutorial.org/book/following_users遵循與狀態

它的正常工作的幫助,下面的功能,但我添加了額外的柱,域名狀態爲其中存在後續的請求被接受一個布爾值。 current_user.following返回current_user的所有關係,但我需要那些關係狀態爲true的用戶。 我的代碼與提到的文章完全一樣,只是我在關係表中添加了狀態列。 所以好心幫我

回答

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

仍然返回current_user與狀態true和false的所有關係.. – User101

+0

這是我的壞..它的工作現在感謝.. – User101

+0

不客氣:) – hoangdd