简体   繁体   English

Rails嵌套N + 1查询问题

[英]Rails Nested N+1 Query Issue

My Associations are like this: 我的协会是这样的:

vendor has shipments  
shipment has order
order has products

In My controller I have written as 在我的控制器中,我写了

@shipments = @vendor.shipments.includes(:order)

But in my view I am using like 但在我看来,我正在使用

shipment.order.products.collect(&:name)

So it is returning N+1 Query issue by Rails 'bullet' gem 所以它正在返回Rails'bullet'gem的N + 1 Query问题

Anyone Help me to resolve this problem of Nested N+1 Query Issue? 任何人帮我解决嵌套N + 1查询问题? How I need to write in the controller? 我怎么需要在控制器中写?

@shipments = @vendor.shipments.includes(:order => :products)

should work. 应该管用。 Read more about it here http://guides.rubyonrails.org/active_record_querying.html#nested-associations-hash 在这里阅读更多相关信息http://guides.rubyonrails.org/active_record_querying.html#nested-associations-hash

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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