-
-
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
Polymorphic associations do not support computing the class #793
Comments
The error occurs in this line in the If change the order of the association definition from: class Comment < ActiveRecord::Base
belongs_to :commentable, polymorphic: true
belongs_to :user
end to: class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :commentable, polymorphic: true
end the issue will be resolved but it's just a temporary life hack 😄 |
This will only fix the issue if you check user attribute in your case. |
I got this error with the devise_invitable gem which adds the |
The issue seems to be caused by this change: |
I have the same issue, downgraded for now to 3.3.0 |
Can confirm, the new offending behaviour is:
For ActiveRecord::Reflection::BelongsToReflection, 'klass' cannot be calculated for polymorphic associations so if one is utilised on the child class, the finder will error. As 'find' halts on the first instance, the temporary fix above to reoder polymorphic associations solves the issue for some. Suggestion would be to add 'unless association.polymorphic?' to the finder to ensure polymorphic associations are ignored. |
Any luck with this? I am currently facing this issue and not finding a good work around so far. |
Unfortunately I did not implement any Work around except for downgrading to version 3.3.0. However, with a little experimenting I found some workaround for my situation, although not ideal. My original Ability code was:
So, this one triggers the above exception with version 3.4.0. I can rewrite it to
Now the exception is gone away and the right authorization is applied to the individual records BUT there is one drawback: now the :read does not apply to the listing anymore. In other words: @users gives me all users and not just that of the account they belong to. So, I should apply an extra filter in my controller to make it work. But that could be acceptable. Hopes this helps for your situation. |
I ran into this same issue and I think I can explain why changing the order of the association works, but I don't know what the long term fix is. The issue seems to stem from
When you do something like On each of the The problem is this rails/rails@fb86ecd doesn't allow you to call The reason the code will work if you re-arrange your associations, is that if the association you're dealing with happens to be near the top and matches BEFORE it hits a polymorphic association, the loop stops. I'm assuming So re-arranging your associations may work - but it's quite brittle and won't work in a lot of cases. Long term fix would be to update this loop code to handle polymorphic associations. |
@caseyli please see my PR linked above. It does just as you suggest and adds support for polymorphic associations. |
Here it is for convenience: #814 |
Closed on 3.5.0. Thank you! |
Steps to reproduce
https://gist.github.com/chubchenko/883eb5a7c49b194a223be3843774670f
Expected behavior
Does not raise an exception
ArgumentError: Polymorphic associations do not support computing the class
Actual behavior
Raises an exception:
System configuration
Rails version: 7.0.3
Ruby version: 3.1.2
CanCanCan version 3.4.0
The text was updated successfully, but these errors were encountered: