Of possible interest for integration into this gem:
https://gist.github.com/TikiTDO/bda28aa2bc387489e2af
Note: This one assumes that all polymorphic relations in a poly int model are integer types. It will need extra checking if you want to generalize behavior any more.