title: Extending ActiveStorage::Attachment - Adding custom fields
body: <p>I want to extend the class ActiveStorage::Attachment and add an enum attribute for visibility of attachments. </p>
  
  <p>My initial approach was to create a new file attachment.rb in the \app\models directory as follows.</p>
  
  <pre><code>class ActiveStorage::Attachment &lt; ActiveRecord::Base
      enum visibility: [ :privately_visible, :publicly_visible]
  end
  </code></pre>
  
  <p>This doesn't work. </p>
  
  <p>Any suggestions are welcome. What's the Rails way to extend classes?</p>
  
  <p><strong>Update</strong></p>
  
  <p>I have a solution that works partially now. 
  For this, I have created an extension active_storage_attachment_extension.rb and placed it in \lib</p>
  
  <pre><code>module ActiveStorageAttachmentExtension
  
    extend ActiveSupport::Concern
  
    included do
      enum visibility: [ :privately_visible, :publicly_visible]
  
      def describe_me
        puts "I am part of the extension"
      end
  
    end
  end
  </code></pre>
  
  <p>The extension is loaded during initialization in extensions.rb</p>
  
  <pre><code>ActiveStorage::Attachment.send(:include, ::ActiveStorageAttachmentExtension)
  </code></pre>
  
  <p>Unfortunately, it is only working partly:
  While the enum methods publicly_visible? and privately_visible? are available in the views, they are not available in the controller. When invoking any of the methods in the controller, then the enum seems to have disappeared. I get a "NoMethodError - undefined method" error. 
  Surprisingly, once the enum methods are called once in the controller, they are also not available any more in the views. 
  I assume that the ActiveStorage::Attachment class gets reloaded dynamically and that the extensions are lost as they are only added during initialization. </p>
  
  <p>Any ideas? </p>