Pulling multiple levels of data efficiently in Rails

眉间皱痕 提交于 2019-12-24 06:58:41

问题


I am trying to build a print process, which consists of printing a batch of financial applications using Rails.

I am printing around 100 applications, which consist of multiple levels of data (the application itself, sub-models, and their sub-models).

At the moment the page is very inefficient as it is doing a lot of N+1 querying which is causing the performance to be poor.

Question is, is there an efficient way of getting this data out of the database. I've tried pulling the forms with includes() for all the sub-models, but this doesn't help with the models that are below that (for instance, income_line_items on a financial_history model)

Any ideas?


回答1:


Have you tried using a nested hash to access the sub-sub-models? Something like:

@app = Application.includes(:first_submodel, 
                            {:second_submodel => [:first_sub_submodel, :second_sub_submodel]},
                            {:third_submodel  => :third_sub_submodel})


来源:https://stackoverflow.com/questions/4829164/pulling-multiple-levels-of-data-efficiently-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!