How do I serialize permissions with active_model_serializers? I don’t have access to current_user or the can? method in models and serializers.
How do I serialize permissions with active_model_serializers? I don’t have access to current_user or
Share
First, to get access to the
current_userin the serializer context, use the new scope feature:In case you are instantiating serializers manually, be sure to pass the scope:
Then inside the serializer, add custom methods to add your own authorization pseudo-attributes, using
scope(the current user) to determine permissions.If you are using CanCan, you can instantiate your Ability class to access the
can?method: