2017-04-07 109 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