Rails中有效地拉动多层次的数据 [英] Pulling multiple levels of data efficiently in Rails
问题描述
我想建立一个印刷过程中,它由印刷了一批使用Rails的金融应用程序。
I am trying to build a print process, which consists of printing a batch of financial applications using Rails.
我打印约100个应用程序,其中包含多个级别的数据(应用程序本身,子模型,其子模型)。
I am printing around 100 applications, which consist of multiple levels of data (the application itself, sub-models, and their sub-models).
目前的页面是非常低效的,因为它是做了很多的N + 1查询这是造成性能较差。
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.
问题是,有没有得到这个数据从数据库中的有效方式。我试着拉了形式与包括()
所有子模型,但是这并不能与那些低于模型帮助(例如, income_line_items
在 financial_history
模型)
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)
任何想法?
推荐答案
您是否尝试过使用嵌套哈希访问子子模型?是这样的:
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})
这篇关于Rails中有效地拉动多层次的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!