Fix has_many_through_join

Check the relation between middle and endpoint table and swap order of join attributes if necessary.