Intersection of join models?

We have models A, B, C such that

A has_many B :through C

Given a in A, b in B, is there a direct way to ask for the cs in C
that belong to both without using explicit IDs in AR calls? I got

a.cs & b.cs

but involves two queries.

– fxn

Xavier N. wrote:

We have models A, B, C such that

A has_many B :through C

Given a in A, b in B, is there a direct way to ask for the cs in C
that belong to both without using explicit IDs in AR calls? I got

a.cs & b.cs

but involves two queries.

– fxn

Is gather the query you are after is something like

select * from C where a_id = :aid and b_id = :bid

Why would you want to avoid using the id’s?

Stephan

On Jan 17, 2007, at 8:28 PM, Stephan W. wrote:

select * from C where a_id = :aid and b_id = :bid

Why would you want to avoid using the id’s?

Because AR is designed in such a way that using an _id column
explicitely for simple things triggers a warning in my head, since it
usually means I am not using the abstractions correctly.

We can here use #detect on a.cs, or a dynamic finder a.cs.find_by_b_id
(b.id), but looks non-idiomatic, so I asked whether I am missing
something. Perhaps in this particular case there is no prepackaged
way to do this, in which case I’ll remove the auto-warning :-).

– fxn