Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Easy way to solve N+1? #208

Closed
blimey85 opened this issue Apr 10, 2016 · 1 comment
Closed

Question: Easy way to solve N+1? #208

blimey85 opened this issue Apr 10, 2016 · 1 comment

Comments

@blimey85
Copy link

I was going to post this on StackOverflow but since it's rather specific to this gem, I thought I'd ask here first. Hopefully that's ok. I have closure_tree working quite nicely for a comments system. If I go to a status, all comments on that status will show and that part works great. If I go to the news feed, I show 10 statuses at a time, and then it calls hash_tree 10 times which means 10 database hits.

Is it possible using joins and includes to move this into my initial query for statuses? Something like:

@statuses = Status.where('something here').joins('sql for closure_tree).includes(:comments)

If it is possible I can work out what code I need to do it, I'm just not sure if it's possible. The end result is I'm wanting to call status.comments.hash_tree, or something similar, and have it via the collection.

@smedstadc
Copy link

I know this is an old issue, but this may help someone else.

You should be able to include one of the scopes listed here: https://github.com/mceachen/closure_tree#instance-methods

If ActiveRecord complains about undefined tables, you might need to add .references(:modelname_hierarchies).

It pays to RTFM 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants