In an API controller, I'd like to limit what fields of a model can be seen depending on who is logged in. ActiveModel Serializers would seem to allow this, but I've had no luck with the following:
class MyModelSerializer < ActiveModel::Serializer
  attributes :name, :custom_field, :secret_field
  has_many :linked_records
  def custom_field
    object.do_something
  end
  def filter(keys)
    unless scope.is_admin?
      keys.delete :secret_field
      keys.delete :linked_records
    end
    keys
  end
end
But, the filtering is never performed and so my output always contains :secret_field and :linked_records even if there's no user logged in.
Perhaps this is because I am using Rails 6, and it would seem that ActiveModel Serializers might no longer be the best tool (e.g. https://stevenyue.com/blogs/migrating-active-model-serializers-to-jserializer).
Please do offer your suggestions for a means to perform this, if you can think of a better means.
EDIT:
Further to all the comments below, here's some different code:
  attributes :name, :id, :admin_only_field, :is_admin
  $admin_only = %i[:id, :admin_only_field]
  def attributes(*args)
    hash = super
    $admin_only.each do |key|
      unless scope.is_admin?
        hash.delete(key)
      end
    end
    hash
  end
  def is_admin
    if scope.is_admin?
      'admin!'
    else
      'not an admin!'
    end
  end
If I then visit the model's index page without being an admin I see that the admin_only_field and id are both present, and is_admin says that I'm not. Bizarre.
 
    