Improving has many though :include

The way has many :through associations are preloaded is currently not ideal. If a use has_many subscriptions and has_many journals through subscriptions and we include subscriptions then we first load subscriptions. Then we load the journals for all the subscriptions and iterate over the subscriptions grabbing journal off each one. This means for example that if you had an order clause on journals then that won't be honoured in any useful way (because we iterate over subscriptions, which are already in some predefined order).

This is a bit disorderly (chortle chortle) and messes up some other stuff I've been playing with so I'd like preloading to do something a lot closer to what happens if the association is loaded normally. There are a fair few cases to consider (polymorphic associations and the plethora of options you can set) which was probably why I originally wrote it the way it is now. It would be great to share the code with has many through as this should help stop the two diverging. As I said it's a fair chunk of code. It's currently broken up into several bits (construct_joins, construct_conditions etc...), would an acceptable path be to pull that out into something which either takes a single owner (ie produces WHERE foo_id= x) or (the preload case) an array of orders which both the association and the preload code would call?

Opinions much appreciated,