Find all records which have a count of an association greater than zero

后端 未结 10 1624
北海茫月
北海茫月 2020-11-30 17:45

I\'m trying to do something that I thought it would be simple but it seems not to be.

I have a project model that has many vacancies.

class Project &         


        
10条回答
  •  执笔经年
    2020-11-30 18:17

    In Rails 4+, you can also use includes or eager_load to get the same answer:

    Project.includes(:vacancies).references(:vacancies).
            where.not(vacancies: {id: nil})
    
    Project.eager_load(:vacancies).where.not(vacancies: {id: nil})
    

提交回复
热议问题