Find records where join doesn't exist

后端 未结 3 1778
死守一世寂寞
死守一世寂寞 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:34

    And if you want to do EXISTS query in elegant and Rails-ish manner, you can use Where Exists gem I've written:

    Question.where_not_exists(:votes, user_id: current_user.id)
    

    Of course, you can made scope of it as well:

    scope :unanswered_by, ->(user){ where_not_exists(:votes, user_id: user.id) }
    

提交回复
热议问题