Nested has_many queries in AR?

Aaa has_many Bbb and Bbb has_many Ccc. There is no :through involved.
Ccc table does not have a FK back to Aaa. (Legacy schema.)

An instance of aaa can easily query with aaa.bbb.find (or .paginate),
but aaa.bbb.ccc.paginate() doesn’t work because Aaa has no knowledge of
Ccc.

What’s the idiomatic way of dealing with this? Loop through
aaa.bbb.paginate results and generate N queries for Ccc and flatten the
array? Seems too brutish.

Not finding great answers via Google, everyone seems to do has_many
:through, and that just doesn’t apply here.

Oh – Rails 3, but using WillPaginate. If there’s a paginate()
compatible way to do this, I need that. Second choice would be to use an
AR3 way to do it that won’t work with paginate(), and I can hack my own
pagination.

TIA for pointers.

– gw

Greg W. wrote in post #955551:

Aaa has_many Bbb and Bbb has_many Ccc. There is no :through involved.
Ccc table does not have a FK back to Aaa. (Legacy schema.)

An instance of aaa can easily query with aaa.bbb.find (or .paginate),
but aaa.bbb.ccc.paginate() doesn’t work because Aaa has no knowledge of
Ccc.

No, that’s not why it doesn’t work. It doesn’t work because aaa.bbb is
an Array (or actually an AssociationProxy, but don’t worry about that),
not a Bbb object. Therefore it has no ccc method the way a Bbb object
would.

What you need is Aaa has_many :cccs, :through => :bbbs. No DB changes
will be involved.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On 19 October 2010 21:06, Greg W. [email protected] wrote:

Aaa has_many Bbb and Bbb has_many Ccc. There is no :through involved.
Ccc table does not have a FK back to Aaa. (Legacy schema.)

I do not understand what you mean by ‘there is no through involved’.
In the situation described you may say
Aaa :has_many :cccs, :through => :bbbs
then you may use aaa.cccs

Colin

Colin L. wrote in post #955556:

On 19 October 2010 21:06, Greg W. [email protected] wrote:

Aaa has_many Bbb and Bbb has_many Ccc. There is no :through involved.
Ccc table does not have a FK back to Aaa. (Legacy schema.)

I do not understand what you mean by ‘there is no through involved’.
In the situation described you may say
Aaa :has_many :cccs, :through => :bbbs
then you may use aaa.cccs

My existing schema does not match the example laid out here:

I do not have one table capable of pointing in parallel to two tables as
diagramed. The schema I am working with is strictly A->B->C and not
A->B and A->C.

But, I’ll give it a try as you guys seem to think it should still work.

– gw

Greg W. wrote in post #955558:

Colin L. wrote in post #955556:

On 19 October 2010 21:06, Greg W. [email protected] wrote:

Aaa has_many Bbb and Bbb has_many Ccc. There is no :through involved.
Ccc table does not have a FK back to Aaa. (Legacy schema.)

I do not understand what you mean by ‘there is no through involved’.
In the situation described you may say
Aaa :has_many :cccs, :through => :bbbs
then you may use aaa.cccs

My existing schema does not match the example laid out here:

That example contains some irrelevant key fields, aimed more at a
habtm-type setup than what you’re doing.

What you’re doing is described exactly in the last paragraph of the
section you referred to, starting at ‘The has_many :through association
is also useful for setting up “shortcuts”…’. The example there
matches your use case exactly: Document is A, Section is B, and
Paragraph is C.

I do not have one table capable of pointing in parallel to two tables as
diagramed.

No. You don’t need one. The A class points to 2 tables, the “as”
table does not have to.

The schema I am working with is strictly A->B->C and not
A->B and A->C.

Exactly as given in the part of the guide I pointed to.

But, I’ll give it a try as you guys seem to think it should still work.

Indeed it should. You’re describing a classic use case for has_many
:through.

– gw

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On 19 October 2010 21:29, Greg W. [email protected] wrote:

My existing schema does not match the example laid out here:

I do not have one table capable of pointing in parallel to two tables as
diagramed. The schema I am working with is strictly A->B->C and not
A->B and A->C.

It is unfortunate that the guide does not have a diagram for the
situation you describe as it is a very common case. It is described
in the text just below but there is no diagram to draw one’s attention
to it.

Colin

Marnen Laibow-Koser wrote in post #955562:

That example contains some irrelevant key fields, aimed more at a
habtm-type setup than what you’re doing.

What you’re doing is described exactly in the last paragraph of the
section you referred to, starting at ‘The has_many :through association
is also useful for setting up “shortcuts”…’. The example there
matches your use case exactly: Document is A, Section is B, and
Paragraph is C.

Hmm. Yeah that seems obvious now. I guess I obsessed on the diagram
(ooo, pretty picture!) and didn’t really digest that lat paragraph.
Thanks!

– gw