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

后端 未结 10 1653
北海茫月
北海茫月 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:19

    Yeah, vacancies is not a field in the join. I believe you want:

    Project.joins(:vacancies).group("projects.id").having("count(vacancies.id)>0")
    

提交回复
热议问题