Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

has_children? performing extra query #63

Open
jgadbois opened this Issue · 2 comments

2 participants

@jgadbois

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.

@moiristo

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?

@jgadbois

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

Model.includes(:children).roots

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.