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

后端 未结 6 1161
星月不相逢
星月不相逢 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:14

    As of Rails 5.1, uniq is deprecated and distinct should be used instead.

    Parent.joins(:children).distinct
    

    This is a follow-up on Chris Bailey's answer. .all is removed as well from the original answer as it doesn't add anything.

提交回复
热议问题