Find records where join doesn't exist

后端 未结 3 1777
死守一世寂寞
死守一世寂寞 2020-11-30 07:19

I have a scope to limit all questions by whether or not a user has voted on them. In the model:

scope :answered_by, lambda {|u| joins(:votes).wh         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 07:47

    try this and let me know if it works

    EDIT-1

    scope :unanswered_questions, lambda { joins('LEFT OUTER JOIN votes ON questions.id = votes.question_id').where('votes.question_id IS NULL') }
    

    EDIT-2

    scope :unanswered_by, lambda {|u| where("questions.id NOT IN (SELECT votes.question_id from votes where votes.user_id = ?)",u.id) }
    

提交回复
热议问题