Add ActiveRecord::Relation#structurally_compatible?
.
#41841
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Adds a query method by which a user can tell if the relation that they're about to use for
#or
or#and
is structurally compatible with the receiver.Other Information
Previously you either needed to iterate over the values of the relation or just catch the ArgumentError thrown by
#or
or#and
in order to determine if two relations were structurally compatible.As for my use case: I have filters coming in from query parameters that are transformed into
ActiveRecord::Relation
objects. Those objects are then joined together usingor
andand
to get to one single relation at the end. The issue is, if a developer adds a join into any of the transformations, then all of the sudden we've got structurally incompatible relations and some filters can't be used with others. What I'd like to do in this case is:or something to that effect, so that I don't have to catch any errors and devs can use whatever grouping/joins they want to to get the filters working correctly.