Tuesday, 6 August 2013

Rails 4 scope to find parents with no children

Rails 4 scope to find parents with no children

I found one answer that had some usable having examples for finding
parents with n children, but the same is not usable for finding parents
with no children (presumably since the join excludes them).
scope :with_children,
joins(:children).group("child_join_table.parent_id").having("count(child_join_table.parent_id)
> 0")
Can anyone point me in the right direction?

No comments:

Post a Comment