Use relation.and
instead of relation.merge
whenever possible.
#695
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.
Currently cancancan uses
relation.merge
to apply scope in ability rule to the scope ofload_resource through
.However, since
relation.merge
intends to mimic Hash#merge,(i.e. later condition on the same column replaces the previous one),
this causes the
load_resource through
become ineffective when the scope in ability also specifics conditions on the same column.This PR adds model adapter for ActiveRecord >= 6.1 and overrides
database_records
to userelation.and
instead. It also tries to keep the query simple by only wrapping scopes to sub queries when necessary, which relies on private ActiveRecord APIstructurally_incompatible_values_for
Example:
ability file:
controller:
References
For more information on the behavior of
relation.merge
andrelation.and
that was introduced in rails 6.1,please refer to rails/rails#39250 & rails/rails#40944