title: Rails 5.2 ActiveStorage with UUIDs on Postgresql
body: <p>We have our app using uuids are primary keys, on a Postgresql Database. (Standard setup described <a href="https://lab.io/articles/2017/04/13/uuids-rails-5-1/" rel="noreferrer">here</a>). </p>
  
  <p>We integrated ActiveStorage following the process described <a href="https://edgeguides.rubyonrails.org/active_storage_overview.html" rel="noreferrer">here</a>. A standard setup using <code>rails active_storage:install</code> and migrated using <code>rails db:migrate</code>.</p>
  
  <p>We have a model &amp; corresponding controller as follows:</p>
  
  <pre><code># Model
  class Message &lt; ApplicationRecord
    has_one_attached :image
  
    def filename
      image&amp;.attachment&amp;.blob&amp;.filename
    end
  end
  
  # Controller
  class MessagesController &lt; ApplicationController
    def create
      message = Message.create!(message_params)
      redirect_to message
    end
  
    private
      def message_params
        params.require(:message).permit(:title, :content, :image)
      end
  end
  </code></pre>
  
  <p>We observed that the first few sets of image were correctly associated with the model instances, but then we used to get random images for model instance, or got no image at all. Every time, we restart the server, we get first few images right, but then it was unpredictable.</p>
  
  <p>Unsure, of what's going wrong, we debugged in rails console:</p>
  
  <pre><code>params[:image]
  =&gt; #&lt;ActionDispatch::Http::UploadedFile:0x007fcf2fa97b70 @tempfile=#&lt;Tempfile:/var/folders/dt/05ncjr6s52ggc4bk6fs521qw0000gn/T/RackMultipart20180726-8503-vg36kz.pdf&gt;, @original_filename="sample.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"sample.pdf\"\r\nContent-Type: application/pdf\r\n"&gt;
  </code></pre>
  
  <p>On saving the instance and retrieving file name we got a random file, we uploaded previously.</p>
  
  <pre><code>@message = Message.new(message_params)
  @message.filename
  =&gt; #&lt;ActiveStorage::Filename:0x007fcf32cfd9e8 @filename="sample.pdf"&gt;
  
  @message.save
  
  @message.filename
  =&gt; #&lt;ActiveStorage::Filename:0x007f82f2ad4ef0 @filename="OtherSamplePdf.pdf"&gt; 
  </code></pre>
  
  <p>Looking for an explanation for this weird behaviour, and a possible solution too.</p>