简体   繁体   中英

N+1 Query how to use includes

If I have a n+1 query problem, but none of the models have a controller, I just use the association to get what I want. What can I do.

DB schema:

Project belongs_to domain Domain has_many projects

Domain has_many domain_datas Domain_datas belongs_to domain

Domain_datas has a column called index so in order to get the index I do this in a view:

project.domain.domain_datas.index

The following should get you all of those models fully pre-loaded:

 Project.includes(domain: {domain_datas: :index})

Also see "13.1 Eager Loading Multiple Associations" on: http://guides.rubyonrails.org/active_record_querying.html

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