title: How to turn an attribute using Active Storage OPTIONAL (AWS S3 and ActiveStorage)
body: <p>I am building an application using activestorage and AWS S3 to storage files. I want the avatar to be optional in this application, however after setup AWS S3 and put the "has_one_attached :avatar" on my User model I realised it is turned the avatar attribute mandatory.</p>
  
  <p>rails routes:</p>
  
  <pre><code>Rails.application.routes.draw do
    devise_for :users, defaults: { format: :json }
  end
  </code></pre>
  
  <p>User model:</p>
  
  <pre><code>class User &lt; ApplicationRecord
    has_many :companies
    has_one_attached :photo
  end
  </code></pre>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>require "application_responder"
  
  class ApplicationController &lt; ActionController::API
  self.responder = ApplicationResponder
  respond_to :html, :json
  
  before_action :configure_permitted_parameters, if: :devise_controller?
  before_action :configure_account_update_params, only: [:update]
  before_action :authenticate_user!, only: [:update]
  before_action :configure_sign_in_params, only: [:create]
    
  include Pundit
  
  # Pundit: white-list approach.
  after_action :verify_authorized, except: :index, unless: :skip_pundit?
  after_action :verify_policy_scoped, only: :index, unless: :skip_pundit?
  
  # Uncomment when you *really understand* Pundit!
  rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
  def user_not_authorized
  flash[:alert] = "You are not authorized to perform this action."
  redirect_to(root_path)
  end
  
  private
  
  def skip_pundit?
  devise_controller? || params[:controller] =~ /(^(rails_)?admin)|(^pages$)/
  end
  
  protected
  
  def configure_permitted_parameters
  devise_parameter_sanitizer.permit(:sign_up, keys: [:cpf, :full_name, :phone_number, :password, :password_confirmation])
  end
  
  def configure_account_update_params
  devise_parameter_sanitizer.permit(:account_update, keys: [
  :full_name, :phone_number, :photo, :nick_name, :email, :current_password, :password, :password_confirmation])
  end
  
  def configure_sign_in_params
  devise_parameter_sanitizer.permit(:sign_in, keys: [:login])
  end
  
  Application Controller:</code></pre>
  </div>
  </div>
  </p>
  
  <p>I already read the active storage docs and there isn't a solution for this point there. <a href="https://guides.rubyonrails.org/active_storage_overview.html" rel="nofollow noreferrer">https://guides.rubyonrails.org/active_storage_overview.html</a></p>
  
  <pre><code>Application:
  ruby 2.6.3
  Rails 6.0.2.1
  Active storage: I already run rails active_storage:install
  rails db:migrate
  </code></pre>
  
  <p><strong>Actual behavior:</strong>
  If I try to create a user without avatar I have a 500 error in the end of the post.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>Started POST "/users" for ::1 at 2020-02-17 21:22:30 -0300
  Processing by Devise::RegistrationsController#create as JSON
    Parameters: {"user"=&gt;{"cpf"=&gt;"75816927019", "password"=&gt;"[FILTERED]", "phone_number"=&gt;"79989972620", "full_name"=&gt;"Mario de Andrade"}, "registration"=&gt;{"user"=&gt;{"cpf"=&gt;"75816927019", "password"=&gt;"[FILTERED]", "phone_number"=&gt;"79989972620", "full_name"=&gt;"Mario de Andrade"}}}
     (0.2ms)  BEGIN
    User Exists? (1.4ms)  SELECT 1 AS one FROM "users" WHERE "users"."phone_number" = $1 LIMIT $2  [["phone_number", "79989972620"], ["LIMIT", 1]]
    User Exists? (0.3ms)  SELECT 1 AS one FROM "users" WHERE "users"."cpf" = $1 LIMIT $2  [["cpf", "75816927019"], ["LIMIT", 1]]
     (0.8ms)  SELECT COUNT(*) FROM "users" WHERE "users"."authentication_token" = $1  [["authentication_token", "fS3SvuYXgH4B89n-khRX"]]
    User Create (0.6ms)  INSERT INTO "users" ("encrypted_password", "created_at", "updated_at", "status", "phone_number", "cpf", "full_name", "authentication_token") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["encrypted_password", "$2a$11$gi3ee1iRFRzzJzdPHHUvZ.rDsWGRlqBTU6aGtWylCBbK2NwwfUCim"], ["created_at", "2020-02-18 00:22:31.368473"], ["updated_at", "2020-02-18 00:22:31.368473"], ["status", "Ativo"], ["phone_number", "79989972620"], ["cpf", "75816927019"], ["full_name", "Mario de Andrade"], ["authentication_token", "fS3SvuYXgH4B89n-khRX"]]
     (2.4ms)  COMMIT
    User Load (0.8ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 23], ["LIMIT", 1]]
    ActiveStorage::Attachment Load (0.7ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4  [["record_id", 26], ["record_type", "User"], ["name", "photo"], ["LIMIT", 1]]
  Completed 500 Internal Server Error in 508ms (ActiveRecord: 31.3ms | Allocations: 68570)
  
  
    
  SystemStackError (stack level too deep):
    
  activestorage (6.0.2.1) lib/active_storage/attached/one.rb:13:in `public_send'
  activestorage (6.0.2.1) lib/active_storage/attached/one.rb:13:in `attachment'
  activesupport (6.0.2.1) lib/active_support/core_ext/module/delegation.rb:293:in `respond_to_missing?'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:54:in `respond_to?'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:54:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
  activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
  activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
  activesupport (6.0.2.1)</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Desired behavior:</strong>
  Create a new user without being mandatory upload an :avatar, however if the user decide to upload the :avatar use activestorage to send and storage on AWS S3.</p>
  
  <p>Someone could help me to customize this behavior?</p>