祖先-如何让所有没有孩子的父母? [英] Ancestry - how to get all parent without children?
本文介绍了祖先-如何让所有没有孩子的父母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我这样做:
@disabled_options = []
Category.where('ancestry is NULL').each do |cat|
@disabled_options << cat.id if cat.has_children?
end
有没有更优雅的方法让所有没有孩子的父母成家?
Is there any more elegant way to get all parent without children?
推荐答案
Category.where("id IN (SELECT parent_id FROM categories)")
假定 parent_id
是指向父类别的字段。
Assuming parent_id
is a field pointing to the parent category.
这将选择使用 parent_id指向的类别,因此,如果有子代,则该子代将设置 parent_id,因此, parent_id有孩子。
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.
这篇关于祖先-如何让所有没有孩子的父母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文