Part of my "week of open source" [0], today I released union_of. This gem for Ruby on Rails allows you to create Active Record associations that combine other associations using SQL UNION under the hood. It has full support for joins, preloading, etc., because the UNION association is a real association. That also means it can also be used for complex through-union associations.<p>I've been working on this gem for about the last year [1]. It took me around 9 months to implement in a performant way, and I'd say it's the pinnacle of my Ruby career. It's definitely some of the most complex code I've ever written, touching so much surface area of the Rails internals. It helped me successfully make it through one of the most complex data migrations I've ever had to do, and all without breaking backwards compatibility. Many failures [2], but ultimately I got to scream "I am invincible!" when all my tests passed.<p>I built this to facilitate migrating from a one-to-many relationship to a many-to-many relationship in a backwards compatible way. A classic problem a lot of us have experienced as well. And it's been used in production to performantly query millions and millions of rows.<p>[0]: <a href="https://x.com/_m27e/status/1823029064372031586" rel="nofollow">https://x.com/_m27e/status/1823029064372031586</a><p>[1]: <a href="https://github.com/keygen-sh/keygen-api/issues/534#issuecomment-1858579965">https://github.com/keygen-sh/keygen-api/issues/534#issuecomm...</a><p>[2]: <a href="https://github.com/keygen-sh/keygen-api/pull/802">https://github.com/keygen-sh/keygen-api/pull/802</a>