简体   繁体   中英

Rails joins + includes of same table?

In a project at work I see a lot of code like

SomeModel.includes(:comments).joins(:comments).[...] # many more where and order clauses, other stuff

My question is: Does it make any sense to use includes and joins on the same table like :comments ?

It may make sense. These two methods serve different purposes. You need joins if you want to filter Posts by attributes of their Comments. And you need includes to avoid N+1 queries when rendering posts with comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM