Rails: How to get objects with at least one child?

后端 未结 6 1140
星月不相逢
星月不相逢 2021-01-03 21:10

After googling, browsing SO and reading, there doesn\'t seem to be a Rails-style way to efficiently get only those Parent objects which have at leas

6条回答
  •  既然无缘
    2021-01-03 21:13

    I have just modified this solution for your need.

    Parent.joins("left join childrens on childrends.parent_id = parents.id").where("childrents.parent_id is not null")
    

提交回复
热议问题