Ancestry - how to get all parent without children?

我的梦境 提交于 2019-12-06 09:30:29
Category.where("id IN (SELECT parent_id FROM categories)")

Assuming parent_id is a field pointing to the parent category.

This will select those categories that are pointed to using "parent_id", so if there's a child, the child will have "parent_id" set, therefore the category referenced to by "parent_id" has children.

This one-liner may help you.

Category.where(id: Category.pluck(:ancestry).compact.map { |e| e.split('/') }.flatten.uniq)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!