Skip to content

FAQ for acts_as_reportable

poptech edited this page Sep 14, 2010 · 2 revisions

Q. I have a fairly complex domain I am trying to report on and am having
difficulty with report_table. My basic question is about the use of
:include. Please consider:
model_a has many model_b
model_b has many model_c
model_b has many model_d through model_c

When I do:
model_a.report_table(:all, :include => [:model_b])

I get all the columns of these two models.

However, when I do
model_a.report_table(:all, :include => [:model_b, :model_c])
I get an error:
/vendor/rails/activerecord/lib/active_record/association_preload.rb:115

Why is this and can someone espouse further on the proper use of include?

Answer:
you need to use include as a nested query within the associated model. in your example you’re saying (by your query) that model_c belongs to model_a when only model_b does, and model_c belongs to model_b.
e.g.
model_a.report_table(:all, :include => {:model_b => {:only => [:fields,:you,:want], :include => :model_c } })
and you can further drill down into what you want to return from model_c

Clone this wiki locally