Search code examples
active-model-serializers

How to remove parameters from AMS 0.10.0.rc4 Deserialization?


Given the following user_params:

  def user_params
    ActiveModelSerializers::Deserialization.jsonapi_parse!(params.to_unsafe_h)
  end

I am unable to remove a parameter in order to do a specific operation that requires the :current_password to be removed. Previously the line below did the job:

user_params.delete(:current_password) 

However, since implementing 0.10.0.rc4, the operation in question generates ActiveRecord::UnknownAttributeError (unknown attribute 'current_password' for User.)

user.update_without_password(user_params) 

I am unsure if this is a bug or a syntax mistake, so I decided to post on SO instead of their Github repo.


Solution

  • Update #1

    As requested by BF4, I created issue #1610 on AMS Github regarding this question. In the meantime, the original post below is a workaround while a final fix is created.

    Original Post

    I could not find a way to remove the params, but using .except(a) does the trick like in the Update method below:

      # Render the updated User using UserSerializer and the AMS Deserialization.
      def update
        # Check if password is blank, if so, clear :current_password
        # and update without password, else updates password.
        if user_params[:password].blank? || user_params[:current_password].blank?
          user.update_without_password(user_params.except(:current_password,:password))
          render json: user
        else
          if user.update_with_password(user_params)
            render json: user, status: :ok
          else
            render json: user.errors, status: :unprocessable_entity
          end
        end
      end