Automatically build has_many :through filters.

This is a rough proposal to solve #2501. No specs yet, but I've tested it out manually on another project of mine and it seems to work pretty well. Not sure if the setup is optimal, though, so I'd love your feedback.