This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
Issue with accessible_by and ActiveRecord SQL JOIN behavior #1002
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.
I have a bit of roundabout logic for permitting a user to access a given image tagged "private" in my application.
The rules are:
The 2 "can" statements from ability.rb that are relevant here are:
When calling accessible_by on a @user.images.accessible_by(current_ability), the call to @model_class.where(conditions).joins(joins) from cancan/lib/cancan/model_adapters/active_record_adapter.rb line 105 created SQL like this (note that there are some "type in" calls to accommodate polymorphic relationships, as well as other scopes being applies [acts_as_paranoid for one]):
This query doesn't work, however. We need to use an outer left join to return the images for which there are no "join" models, or we only get the models from the second can rule, and not the first. I'm 99% sure that this works in its place:
Because this only gets called in the context of accessible_by, I don't think there would be any other unintended side effects.