fixed has_many_through join function for polymorphic relations

Fixes an issue introduced in f34a38a764120e9163149beb77c37c075752e859, has-many-through polymorphic association stopped working.
This pull request adds a test that demonstrate the issue and code that fix it.