Rails is often touted for “works out of the box”, however the same principle can be quite irksome when you need it to work for your use case. For a long time, it was impossible to declare associations when dealing with a multi-database structure. This is because associations can’t join across databases.
Fortunately, Rails now offers the ability to disable joins when making associations. When this option is set, it Rails to make 2 or more queries rather than using joins.
Let’s look at an example,
Now when we call author.votes
, instead of a using a join to determine the votes accumulated by the author, Rails will do two separate queries.
You can also use the same option to has_one relationships as well!
This is useful in multi-database architectures where associations can not be made across databases. This PR to the Rails codebase goes into further detail.
Prior to this implementation, you’d have to write individual methods that form these relationships instead,
As you can see this new feature really cleans up the code!