Reputation: 48443
I am doing it this way:
@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?
Upvotes: 2
Views: 2617
Reputation: 21
This one-liner may help you.
Category.where(id: Category.pluck(:ancestry).compact.map { |e| e.split('/') }.flatten.uniq)
Upvotes: 2
Reputation: 2213
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.
Upvotes: 1