I have a table A with a polymorphic column that is directly or indirectly associated to table D in three different ways.
A -> D
A -> B -> D
A -> C -> B -> D
A
| id | polymorphic_id |
|---|---|
| 1 | 5 (for table B) |
| 2 | 6 (for table C) |
| 3 | 7 (for table D) |
Here's a simple example of the tables and relevant columns
B
| id | d_id |
|---|---|
| 5 | 7 |
C
| id | b_id |
|---|---|
| 6 | 5 |
D
| id |
|---|
| 7 |
Given A, I want to get all the rows for column id from D
Is there a way I can join these all together? I think some type of UNION is what I'm looking for.
This could be a regular sql query (I'm using postgres) or in Rails 2.