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

여전히 상태가 true 및 false 인 current_user의 모든 관계를 반환합니다 .. – User101

+0

그것은 내 것이고 .. 지금은 감사하고 있습니다 ... – User101

+0

안녕하세요. – hoangdd

관련 문제