Skip to content


Subversion checkout URL

You can clone with
Download ZIP


has_children? performing extra query #63

jgadbois opened this Issue · 2 comments

2 participants


I'm iterating through some results that have_ancestry and checking has_children? This seems to be fetching the children even in cases where the ancestry column is blank. Is there anyway to avoid this? If I show 100 instances of a model on a screen I don't want to have to run that extra query per item.


has_children? does 'self.children.exists?({})'. It might be better to use 'children.any?'. Can you see if this helps by overriding the method in your model?


Looks like children.any? uses a count(*) query instead of selecting the first child. Ideally you could do something like this:


Then you would just have 2 queries, one for the original query and one to fetch children instead of N+1 queries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.