Rails: Find rows without connection in HABTM relation

前端 未结 3 614
悲哀的现实
悲哀的现实 2020-12-29 15:15

I have two models, Users and Leads connected with HABTM relation:

class Lead < ActiveRecord::Base
  has_and_belongs_to_many :use         


        
3条回答
  •  别那么骄傲
    2020-12-29 16:00

    You can use a left join. Assuming your join table is named leads_users:

    Lead.joins('LEFT JOIN leads_users ON leads_users.lead_id = leads.id').
         where(leads_users: { user_id: nil })
    

提交回复
热议问题