问题
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