Inspired by Django’s
get()
method, Rails now introduces #sole
and #find_sole_by
.
The purpose of this query is given a set of constraints,
to match only one unique record in the database.
It can be used when we need to fetch only a single row, but also want to assert that there aren’t multiple rows matching the condition; especially when database constraints aren’t enough or are impractical.
Before
For example, imagine the need to find a user’s API key entry. It is assumed that each API key is unique, but anything can happen!
We might have to do the following,
Now, this is impractical for so many reasons! Like above, where we need to first peek into the length of the results to determine their uniqueness. When extrapolating this to include querying between multiple models, the complexity increases.
Enter #sole
and #find_sole_by
!
After
This commit adds these two query options to ActiveRecord. Let’s check out how they work!
The #find_sole_by
method works similar to #find_by
query,
where it aims to “find” a record matching a constraint.
However, with #find_sole_by
it ensures that this is the unique record matched.
Our previous example can now be reduced to,
The above query will return the ONLY matching record if it exists.
It will raise an ActiveRecord::RecordNotFound
if no record is found, or raises an ActiveRecord::SoleRecordExceeded
if more than one record is found.
Let’s look at another example of #sole
.
This behaves like #first
, but instead of just extracting the first result,
it extracts the unique result matching said constraints.