[Ruby on Rails (RoR)] Cross-cluster associations to Rails 7
Cross-cluster Associations
Most recently, Github Ruby team extracted internal functionality to disable joining queries when an association crossed multiple databases. Prior to our work in this area, Rails had no support for handling associations that spanned across clusters; teams had to write SQL to achieve this.
The overall implementation is relatively small. To accomplish disabling joins, we added an option to has_many :through
associations called disable_joins
. When set to true for an association, Rails will generate separate queries for each database rather than a join query.
Now applications can add the following to their associations to make sure Rails generates two or more queries instead of joins:
1 | class Dog < AnimalsRecord |
And that’s all that’s needed to disable generating joins for associations that cross database servers!
Now, calls to dog.treats will generate the following SQL:
1 | SELECT "humans"."id" FROM "humans" WHERE "humans"."dog_id" = ? [["dog_id", 1]] |