acts_as_reportable basic questions for relatively complex data model

5 views
Skip to first unread message

Halfordian Golfer

unread,
Jun 10, 2009, 9:45:14 PM6/10/09
to Ruby Reports
Hi,

I have a fairly complex domain that I am trying to report on and am
having
difficulty with report_table.

But, my basic question is about the general use of :include.

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

Can someone help me understand this and how includes should be used in
this situation? I'm curious about the need to make sure about the
inclusion of :id column of one model that is used by another,
especially when it is polymorphic through model 'n'.

Many thanks,

Tim

Andrew France

unread,
Jun 11, 2009, 5:16:12 AM6/11/09
to ruby-r...@googlegroups.com
Hi Tim,

Halfordian Golfer wrote:
> But, my basic question is about the general use of :include.
>
> 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])

Model_c is not a relation on model_a, you want a nested include:
:include => {:model_b => {:include => :model_c}}

And if you wanted model_d:
:include => {:model_b => {:include => {:model_c => {:include => :model_d}}}}

Don't ask for the model_d and model_c relations from model_b at the same
level as it will cause duplicates (see many previous messages on list).

>
> I get an error:
> /vendor/rails/activerecord/lib/active_record/association_preload.rb:
> 115

I'm afraid you'd actually have to give the exception message and a few
more lines of backtrace. I suspect it was along the lines of
"association doesn't exist" though, so never mind if that's the case.

> Can someone help me understand this and how includes should be used in
> this situation? I'm curious about the need to make sure about the
> inclusion of :id column of one model that is used by another,
> especially when it is polymorphic through model 'n'.

I don't know what you mean about the id column.

Hope that helps,
Andrew


Halfordian Golfer

unread,
Jun 11, 2009, 8:50:09 AM6/11/09
to Ruby Reports
Hi Andrew,

This is exactly what I was looking for and points me back in the right
direction.

My question about the id's stems from the use of it in various
examples that I may have been reading too much in to. It was looking
like you'd need to ensure the id column was present if it was to be
used as a foreign key in subsequent includes. Its absence causing the
include to fail. Again, most likely, my bad interpretation of the
examples I was looking at.

I'll poke about thus newly armed and see what shakes loose.

Indebted.

Tim

Halfordian Golfer

unread,
Jun 13, 2009, 10:22:15 AM6/13/09
to Ruby Reports
Just another quick question on this.

In the example where:

model_a has many model_b through model_c

Do you need to include model_c?

Thanks,

Tim

Halfordian Golfer

unread,
Jun 13, 2009, 10:23:34 AM6/13/09
to Ruby Reports
Duh. I see that this was answered already. Very helpful!!!

Thanks,

Tim

Halfordian Golfer

unread,
Jun 17, 2009, 10:25:19 AM6/17/09
to Ruby Reports
Been experimenting with this for a while and still getting an error.
Any thoughts much appreciated.

Here's the line and stack trace.

@table = ModelA.report_table(:all, :only =>
[:id, :name], :include => {:model_b => {:only => [:name], :include =>
{:model_c => {:only => [:name], :include => :model_d }}}})

Error: /GIT/apollo/vendor/rails/activerecord/lib/active_record/
association_preload.rb:115:in `preload_one_association'/GIT/apollo/
vendor/rails/activerecord/lib/active_record/association_preload.rb:
114:in `each'/GIT/apollo/vendor/rails/activerecord/lib/active_record/
association_preload.rb:114:in `preload_one_association'/GIT/apollo/
vendor/rails/activerecord/lib/active_record/association_preload.rb:
91:in `preload_associations'/GIT/apollo/vendor/rails/activerecord/lib/
active_record/association_preload.rb:99:in `preload_associations'/GIT/
apollo/vendor/rails/activerecord/lib/active_record/
association_preload.rb:93:in `each'/GIT/apollo/vendor/rails/
activerecord/lib/active_record/association_preload.rb:93:in
`preload_associations'/GIT/apollo/vendor/rails/activerecord/lib/
active_record/association_preload.rb:99:in `preload_associations'/GIT/
apollo/vendor/rails/activerecord/lib/active_record/
association_preload.rb:93:in `each'/GIT/apollo/vendor/rails/
activerecord/lib/active_record/association_preload.rb:93:in
`preload_associations'/GIT/apollo/vendor/rails/activerecord/lib/
active_record/association_preload.rb:90:in `preload_associations'/GIT/
apollo/vendor/rails/activerecord/lib/active_record/
association_preload.rb:90:in `each'/GIT/apollo/vendor/rails/
activerecord/lib/active_record/association_preload.rb:90:in
`preload_associations'/GIT/apollo/vendor/rails/activerecord/lib/
active_record/base.rb:1492:in `find_every'/GIT/apollo/vendor/rails/
activerecord/lib/active_record/base.rb:589:in `find'/Library/Ruby/Gems/
1.8/gems/acts_as_reportable-1.1.1/lib/ruport/acts_as_reportable.rb:
166:in `report_table'/GIT/apollo/test/unit/report_test.rb:61:in
`__bind_1245248419_704714'/Library/Ruby/Gems/1.8/gems/thoughtbot-
shoulda-2.10.1/lib/shoulda/context.rb:253:in `call'/Library/Ruby/Gems/
1.8/gems/thoughtbot-shoulda-2.10.1/lib/shoulda/context.rb:253:in
`test: Given an existing Apollo system with devices installed should
create a pdf account report. '/GIT/apollo/vendor/rails/activesupport/
lib/active_support/testing/setup_and_teardown.rb:94:in `__send__'/GIT/
apollo/vendor/rails/activesupport/lib/active_support/testing/
setup_and_teardown.rb:94:in `run'/System/Library/Frameworks/
Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/test/unit/testsuite.rb:
34:in `run'/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/
lib/ruby/1.8/test/unit/testsuite.rb:33:in `each'/System/Library/
Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/test/unit/
testsuite.rb:33:in `run'/System/Library/Frameworks/Ruby.framework/
Versions/1.8/usr/lib/ruby/1.8/test/unit/testsuite.rb:34:in `run'/
System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/
test/unit/testsuite.rb:33:in `each'/System/Library/Frameworks/
Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/test/unit/testsuite.rb:
33:in `run'/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/
lib/ruby/1.8/test/unit/ui/testrunnermediator.rb:46:in `old_run_suite'/
Applications/RubyMine 1.1.app/rb/testing/patch/test/unit/ui/
testrunnermediator.rb:36:in `run_suite'/Applications/RubyMine 1.1.app/
rb/testing/patch/test/unit/ui/teamcity/testrunner.rb:69:in
`start_mediator'/Applications/RubyMine 1.1.app/rb/testing/patch/test/
unit/ui/teamcity/testrunner.rb:57:in `start'/System/Library/Frameworks/
Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/test/unit/ui/
testrunnerutilities.rb:29:in `run'/System/Library/Frameworks/
Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/test/unit/autorunner.rb:
216:in `run'/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/
lib/ruby/1.8/test/unit/autorunner.rb:12:in `run'/System/Library/
Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/test/unit.rb:
278-e:1
Reply all
Reply to author
Forward
0 new messages