-
-
Notifications
You must be signed in to change notification settings - Fork 638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using Scopes in Ability results in hundreds of queries #511
Comments
https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities You just want can :read, Episode
can :read, Material The |
I don't see what should be done here. The issue seems solved with @duffyjp suggestion |
@coorasse @duffyjp I've used the Episode.all for an example of a scope but this happens with all scopes and that is the problem. The real example from the real project is with:
Which returned the episodes that are published. So removing the scope is not a solution. There could be another scope. If a scope is used than the problem with thousands of queries occurs. If on the other hand a condition is used
than the queries are not printed. But moving a scope to a condition is very hard, and sometimes impossible, because the scope could involve a more complex logic |
@thebravoman I have several large projects that use cancancan extensively. I've made a rule that I only use condition hashes, and forbid blocks. As you get more and more rules, the extra effort is worth it. Ideally, if you have a can :read, Episode, published: true If not, use two rules. can :read, Episode
cannot :read, Episode, published_at: nil |
@duffyjp, But the issue here is that by using blocks an undesired behavior occurs. Should we note that scopes should not be used in cancancan because of issues or it should be able to use scopes. |
Can you please provide a gist to reproduce the error @thebravoman ? You can find it in the pull request template. thanks |
I'v provided a whole project for reproducing it. gist is not enough.
…On Fri, Jul 6, 2018, 07:49 Alessandro Rodi ***@***.***> wrote:
Can you please provide a gist to reproduce the error @thebravoman
<https://github.com/thebravoman> ? You can find it in the pull request
template. thanks
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#511 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABQDBX4P06AWun9iLEzCYYzSHlmLU2yxks5uDuxBgaJpZM4UohMx>
.
|
This is happening to me too
and then literally hundreds of |
Here is what we've done since then.
We've separated the large ability on many small abilities that we are
merging and we are trying to remove scopes from the Abilities. There are
still some but at least we are trying.
…On Wed, 5 Dec 2018 at 11:20, Kabir Sarin ***@***.***> wrote:
This is happening to me too
# tag.rb
class Tag < ApplicationRecord
scope :preset?, -> { where(scenario: nil) }
end
# ability.rb
def initialize
can :read, Tag, id: Tag.preset?.pluck(:id)
end
and then literally *hundreds* of SELECT 1 AS one from "Tags" ...
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#511 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABQDBVwhK4XxVlGBhXCSf7g9GZKyw7Bxks5u14_ogaJpZM4UohMx>
.
--
+359 878 93 30 94
Училище по роботика "Robopartans"
www.robopartans.com
|
I'm not sure how that would help my situation? I need to only allow tags which do not belong to a scenario. If I can't use a block (because it breaks |
1. Don't use cancancan - it is difficult for me to migrate and I kind of
like the qay cancancan could be used, but I know of at least one project
that has stopped using cancancan
2. Migrate the scope to an sql that could be uswd in the ability. if the
are not many joins basically it is doable.
…On Wed, Dec 5, 2018, 19:40 Kabir Sarin ***@***.*** wrote:
I'm not sure how that would help my situation? I need to only allow tags
which do not belong to a scenario to be read. If I can't use a block, and I
can't use a scope, how am I supposed to do this?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#511 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABQDBYwhgKuRqWWsneNTBm1hsBEpN940ks5u2ATxgaJpZM4UohMx>
.
|
Is there some reason this has to happen? Why would using a scope be fundamentally different than writing the raw SQL? Can't you literally call |
Yes. I think it is actually a bug. I can not understand how the scope causes these errors to occur, but if you use an sql statement no errors occur. These are not errors actually - these are 2 minutes long dumps in the log that completely block your server and make you development a nightmare and this happens only if there is a real error - like 500. |
The problem is related to the fact that the scopes are evaluated every time the ability file is used (i.e. a call to |
Looks like this thread is related: rails/rails#30497 |
the fix will be released in version 3.0.0 |
Great. Thanks @coorasse. Is 3.0.0 planned for the near future? |
yes. in the meantime you can try the branch |
I just ran into an issue, unrelated to the original issue raised here, where I had to use use two rules even though I was using a boolean attribute like in your first example. For me this was needed in order to allow accessing Page objects, but not restricted ones.
|
Issue is that when there is an error in the controller or the view then hundreds of queries are output to the console increasing the time waiting when an error occurs to about a minute which is unproductive.
Complete repo for reproducing is available at:
https://github.com/thebravoman/cancan_error
WIth the repo
go to /course_sections/1/edit
The following error occurs
That is not the problem. The problem is that when an error occurs this is output in the console
Here you can see a log of CACHE errors.
This project is a very simple example, but in a real life scenario, especially when the episodes and materials have some logic and globalize installed then hundreds of queries are output to the console and it takes about a minute.
This means that if there is and error on the server durring development we must wait about a minute which is unproductive.
The issue occurs probably because of
If this lines are changed with
The issue will no longer occur
The text was updated successfully, but these errors were encountered: