I have a complex DB schema in my Rails 2.3 app. At some point, I need to make a query to retrieve data from several columns by joining multiple tables. There is a table where I always need to go through to retrieve the data I need.
The problem comes when I try to use Rails syntax to make the joins. I found no way to make rails skip joining the same table again and again.
Let’s say I have this table relationships:
A=>B=>C=>D=>E=>F
A=>B=>F=>G
A=>B=>F=>H
A=>I
As you can see, B is a table that I “join” several times to get to the tables I need.
My query looks something like this:
A.all( :select=>"SOME DATA FROM F, G, H AND I",
:joins=>[{{{{:B=>:C}=>:D}=>:E}=>:F}, {{:B=>:F}=>:G}, {{:B=>:F}=>:H}, :I],
:conditions=>{"SOME CONDITIONS"
}
)
You can see that I use the hash syntax to specify the joins.
The problem is that when I look at the join that rails create, I see that it joins B 3 times. I would expect that it would be smart enough to do it just just once and go from there but I guess that there maybe cases where you want to join several times.
My question is, how can I, using rails 2.3.8 syntax, make so that the resulting query just joins B once. Adding extra relationships in the model of type :belongs_to :throug is not an option for me
try