Home > database >  Rails 6, Paperclip, S3, s3_direct_upload @attachment.save not saving after the s3_direct_upload comp
Rails 6, Paperclip, S3, s3_direct_upload @attachment.save not saving after the s3_direct_upload comp

Time:11-14

I have re-written an application called FilterTrak from an older version of Rails v4.2 to Rails v6 and I have everything working now except one part of the picture upload.

The app uses Paperclip, aws-sdk-v1, and s3_direct_upload. I previously had an issue with s3_direct_upload which I got working. The issue I am having now is after it uploads the file to s3 it is supposed to save the attachment which will give me an ID from the database which then calls a secondary process that is supposed to start.

For reasons unknown the @attachment.save is not saving and because it is not saving it is not making a callback to the second process that is supposed to move the file into its perm location on S3 and then save that information into the DB. I have tried to manually call the second process but because the attachment isn't saving an ID isn't generated. Since I have no ID to work with I can't manually or otherwise call the second process. I believe I have troubleshot the issue to the controller and the @attachment.save line. If I inspect the @attachment.save right after that I get @attachment.save FALSE. This code is currently working as is on an older version of Rails right now. So I assume this part is not working due to some change in the newer version of Rails. I have spent over 2 solid weeks trying to figure this out and I haven't been able to make any progress. I was considering changing the app to use active storage but since the @attachment.save is the part I believe that isn't working I don't know if that would really do any good as the file upload doesn't seem to be where the problem is as far as I can tell. Since it DOES in fact upload to my S3 bucket into the /uploads folder successfully.

Here is the attachments_controller.rb file.

class AttachmentsController < ApplicationController
  load_and_authorize_resource except: [:create]
  skip_authorization_check only: [:create]

  before_action :set_service, only: [:create, :update, :destroy]
  before_action :set_attachment, only: [:update, :destroy]

  def create!
    @attachment = @service.attachments.new(attachment_params)
    @attachment.when = params[:when]
    @attachment.save 
  end

  def update
    respond_to do |format|
      if @attachment.update(attachment_params)
        format.html { redirect_to @attachment, notice: 'Attachment was successfully updated.' }
        format.json { render :show, status: :ok, location: @attachment }
      else
        format.html { render :edit }
        format.json { render json: @attachment.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @attachment.destroy
    respond_to do |format|
      format.html { redirect_to customer_filter_service_path(@customer, @filter, @service), notice: 'Picture was successfully removed.' }
      format.json { head :no_content }
    end
  end

  private
    def set_service
      @customer = Customer.find(params[:customer_id])
      @filter = @customer.filters.find(params[:filter_id])
      @service = @filter.services.find(params[:service_id])
      #@attachment = Attachment.find(params[:id])
    end

    def set_attachment
      @attachment = Attachment.find(params[:id])
    end

    def attachment_params
      params.require(:attachment).permit(:direct_upload_url)
    end
end

Here is the attachments.rb model (Changed the bucket name to mybucket)

# == Schema Information
#
# Table name: attachments
#
#  id                  :integer          not null, primary key
#  service_id          :integer
#  user_id             :integer
#  direct_upload_url   :string           not null
#  upload_file_name    :string
#  upload_content_type :string
#  upload_file_size    :integer
#  upload_updated_at   :datetime
#  processed           :boolean          default("false"), not null
#  created_at          :datetime         not null
#  updated_at          :datetime         not null
#  when                :string
#

class Attachment < ApplicationRecord
  # Environment-specific direct upload url verifier screens for malicious posted upload locations.

  DIRECT_UPLOAD_URL_FORMAT = %r{\Ahttps:\/\/mybucket#{!Rails.env.production? ? "\\-#{Rails.env}" : ''}\.s3\.amazonaws\.com\/(?<path>uploads\/. \/(?<filename>. ))\z}.freeze

  #attr_accessor :id, :service_id, :user_id, :direct_upload_url, :upload_file_name, 
  #:upload_content_type, :upload_file_size, :upload_updated_at, :processed, :created_at, :updated_at, :when       

  belongs_to :service
  belongs_to :user  
  has_attached_file :upload,
    path: 'service/:filter_id/attachments/:id-:style-:filename',
    styles: { medium: "200x200" }
 
  validates :direct_upload_url, presence: true, format: { with: DIRECT_UPLOAD_URL_FORMAT }
  validates_attachment_content_type :upload, :content_type => ["image/jpg", "image/jpeg", "image/png"]
  validates_presence_of :when

  before_create :set_upload_attributes
  after_create :queue_processing
    
  # interpolate in paperclip
  Paperclip.interpolates :filter_id  do |p, _|
    p.instance.service.filter_id
  end

  # Store an unescaped version of the escaped URL that Amazon returns from direct upload.
  def direct_upload_url=(escaped_url)
    write_attribute(:direct_upload_url, (CGI.unescape(escaped_url) rescue nil))
  end
    
  # Determines if file requires post-processing (image resizing, etc)
  def post_process_required?
    %r{^(image|(x-)?application)/(jpeg|jpg|pjpeg|png|x-png)$}.match(upload_content_type).present?
  end
  
  # Final upload processing step
  def self.transfer_and_cleanup(id)
    attachment = Attachment.find(id)
    direct_upload_url_data = DIRECT_UPLOAD_URL_FORMAT.match(attachment.direct_upload_url)

    s3 = AWS::S3.new
    
    if attachment.post_process_required?
      # this has an issue removing brackets
      attachment.upload = URI.parse(URI.escape(attachment.direct_upload_url))
    else
      paperclip_file_path = "service/filter-#{filter_id}/service-#{service_id}/photos/#{direct_upload_url_data[:filename]}"
      s3.buckets[Rails.configuration.aws[:bucket]].objects[paperclip_file_path].copy_from(direct_upload_url_data[:path])
    end
 
    attachment.processed = true
    attachment.save
    
    s3.buckets[Rails.configuration.aws[:bucket]].objects[direct_upload_url_data[:path]].delete
  end

  def before_install?
    self.when == "before"
  end

  def after_install?
    self.when == "after"
  end

  protected
  
  # Set attachment attributes from the direct upload
  # @note Retry logic handles S3 "eventual consistency" lag.
  def set_upload_attributes
    tries ||= 5
    direct_upload_url_data = DIRECT_UPLOAD_URL_FORMAT.match(direct_upload_url)
    s3 = AWS::S3.new
    direct_upload_head = s3.buckets[Rails.configuration.aws[:bucket]].objects[direct_upload_url_data[:path]].head
 
    self.upload_file_name     = direct_upload_url_data[:filename]
    self.upload_file_size     = direct_upload_head.content_length
    self.upload_content_type  = direct_upload_head.content_type
    self.upload_updated_at    = direct_upload_head.last_modified
  rescue AWS::S3::Errors::NoSuchKey => e
    tries -= 1
    if tries > 0
      sleep(3)
      retry
    else
      false
    end
  end
  
  # Queue file processing
  def queue_processing
    # Attachment.delay.transfer_and_cleanup(id)
    Attachment.transfer_and_cleanup(id)

    # bust service cache
    service.touch
  end

  scope :before_install, -> { where( when: "before" ) }
  scope :after_install, -> { where( when: "after" ) }
end

Here is the relevant section of the view file:

%br.visible-print/
    %br.visible-print/
    .panel.panel-default.page-break
      .panel-heading
        .row
          .col-md-6
            .pull-left
              Pictures
      .panel-body
        .row
          .col-md-12
            .row
              .col-md-12
                %h4.strong Before Cleaning Photos
            .row.hidden-print
              .col-md-12
                .well#before-pictures-dropzone.dropzone
                  %h4 Click to Add
                  = s3_uploader_form id: "attachment_before",
                    callback_url: customer_filter_service_attachments_url(@customer, @filter, @service, when: "before"),
                    callback_param: "attachment[direct_upload_url]",
                    key_starts_with: "uploads/",
                    key: "uploads/{timestamp}-{unique_id}-#{SecureRandom.hex}/${filename}",
                    max_file_size: 100.megabytes do
                    = file_field_tag(:file, class: "attachment", multiple: true, data: { url: s3_uploader_url })
                  #before_uploads_container
                  %script#template-upload{:type => "text/x-tmpl"}
                    <div id="upload_{%=o.unique_id%}" class="upload">
                    <h5>{%=o.name%}</h5>
                    <div ><div  style="width: 0%"></div></div>
                    </div>
            .row
              .col-md-12
                - if @before_photos
                  = render @before_photos, gallery: "before"
                - else
                  %h5 No Photos Added Yet
                #before_photos_container

Here is the section in the routes file that deals with the part:

resources :customers do
    resources :filters, except: [:index, :show] do
      resources :services, except: [:index, :show] do
        post 'email', on: :member
        resources :attachments, except: [:index, :show]
      end
    end
  end

Here is the starting of the upload process after you click on the Choose File button and select your file or files and again weather it is 1 file or 10 files they all successfully upload to S3 into the /uploads folder. The progress bars come up, build, completed and then go away like it is supposed to. The second process just never starts up.

Started POST "/customers/419/filters/990/services/1137/attachments?when=before" for ::1 at 2021-11-10 09:07:42 -0800
Processing by AttachmentsController#create as */*
  Parameters: {"url"=>"https://mybucket.s3.amazonaws.com/uploads/1636564061858-fi8lg02378-65280638d6d13515e74449339d1aa926/DES LOGO (512x512).jpg", "filepath"=>"/uploads/1636564061858-fi8lg02378-65280638d6d13515e74449339d1aa926/DES LOGO (512x512).jpg", "filename"=>"DES LOGO (512x512).jpg", "filesize"=>"86383", "lastModifiedDate"=>"Wed May 24 2017 11:55:08 GMT-0700 (Pacific Daylight Time)", "filetype"=>"image/jpeg", "unique_id"=>"fi8lg02378", "attachment"=>{"direct_upload_url"=>"https://mybucket.s3.amazonaws.com/uploads/1636564061858-fi8lg02378-65280638d6d13515e74449339d1aa926/DES LOGO (512x512).jpg"}, "when"=>"before", "customer_id"=>"419", "filter_id"=>"990", "service_id"=>"1137"}
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/json-1.8.6/lib/json/common.rb:155: warning: Using the last argument as keyword parameters is deprecated
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/json-1.8.6/lib/json/common.rb:155: warning: Using the last argument as keyword parameters is deprecated
  User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 34], ["LIMIT", 1]]
  Customer Load (1.0ms)  SELECT "customers".* FROM "customers" WHERE "customers"."id" = $1 LIMIT $2 /*line:/app/controllers/attachments_controller.rb:42:in `set_service'*/  [["id", 419], ["LIMIT", 1]]
   app/controllers/attachments_controller.rb:42:in `set_service'
  Filter Load (0.9ms)  SELECT "filters".* FROM "filters" WHERE "filters"."customer_id" = $1 AND "filters"."id" = $2 LIMIT $3 /*line:/app/controllers/attachments_controller.rb:43:in `set_service'*/  [["customer_id", 419], ["id", 990], ["LIMIT", 1]]
   app/controllers/attachments_controller.rb:43:in `set_service'
  Service Load (1.0ms)  SELECT "services".* FROM "services" WHERE "services"."filter_id" = $1 AND "services"."id" = $2 LIMIT $3 /*line:/app/controllers/attachments_controller.rb:44:in `set_service'*/  [["filter_id", 990], ["id", 1137], ["LIMIT", 1]]
   app/controllers/attachments_controller.rb:44:in `set_service'
Attachment Load (0.9ms)  SELECT "attachments".* FROM "attachments" WHERE "attachments"."service_id" = $1 /*line:/app/controllers/attachments_controller.rb:11:in `create'*/  [["service_id", 1137]]
   app/controllers/attachments_controller.rb:11:in `create'

:: RIGHT HERE IS WHERE A SECOND PROCCESS IS SUPPOSED TO START ::

  Rendering attachments/create.js.erb
  Rendered attachments/create.js.erb (Duration: 0.1ms | Allocations: 8)
Completed 200 OK in 29ms (Views: 1.6ms | ActiveRecord: 4.8ms | Allocations: 10698)

Here is what the second proccess should be, this is taken off of the app that is currently running on Heroku.

2021-10-25T23:13:27.756616 00:00 app[web.1]:  (0.5ms)  BEGIN
2021-10-25T23:13:27.802287 00:00 app[web.1]: [AWS S3 200 0.044684 0 retries] head_object(:bucket_name=>"dpfregen",:key=>"uploads/1635203641183-o14kckjjp6d-1311ed2cf3237c252d35885ba8bbd47a/DES LOGO (512x512).jpg")
2021-10-25T23:13:27.802289 00:00 app[web.1]:
2021-10-25T23:13:27.805869 00:00 app[web.1]: SQL (2.3ms)  INSERT INTO "attachments" ("direct_upload_url", "service_id", "when", "created_at", "updated_at", "upload_file_name", "upload_file_size", "upload_content_type", "upload_updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"  [["direct_upload_url", "https://dpfregen.s3.amazonaws.com/uploads/1635203641183-o14kckjjp6d-1311ed2cf3237c252d35885ba8bbd47a/DES LOGO (512x512).jpg"], ["service_id", 1137], ["when", "before"], ["created_at", "2021-10-25 23:13:27.756915"], ["updated_at", "2021-10-25 23:13:27.756915"], ["upload_file_name", "DES LOGO (512x512).jpg"], ["upload_file_size", 86383], ["upload_content_type", "image/jpeg"], ["upload_updated_at", "2021-10-25 23:13:28.000000"]]
2021-10-25T23:13:27.807045 00:00 app[web.1]: Attachment Load (0.7ms)  SELECT  "attachments".* FROM "attachments" WHERE "attachments"."id" = $1 LIMIT 1  [["id", 106905]]
2021-10-25T23:13:27.834504 00:00 app[web.1]: Service Load (0.9ms)  SELECT  "services".* FROM "services" WHERE "services"."id" = $1 LIMIT 1  [["id", 1137]]
2021-10-25T23:13:27.852128 00:00 app[web.1]: [AWS S3 404 0.016364 0 retries] head_object(:bucket_name=>"dpfregen",:key=>"service/990/attachments/106905-original-DES LOGO (512x512).jpg") AWS::S3::Errors::NoSuchKey No Such Key
2021-10-25T23:13:27.852129 00:00 app[web.1]:
2021-10-25T23:13:27.869707 00:00 app[web.1]: [AWS S3 404 0.017152 0 retries] head_object(:bucket_name=>"dpfregen",:key=>"service/990/attachments/106905-medium-DES LOGO (512x512).jpg") AWS::S3::Errors::NoSuchKey No Such Key
2021-10-25T23:13:27.869708 00:00 app[web.1]:
2021-10-25T23:13:27.871399 00:00 app[web.1]: Command :: file -b --mime '/tmp/019c51a707e3a0c3ea3263003bcd49ce20211025-6-1mcjn6k.jpg'
2021-10-25T23:13:27.874638 00:00 app[web.1]: Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/97bb878daac24e395c0721c1be8733cd20211025-6-1l11e2y.jpg[0]' 2>/dev/null
2021-10-25T23:13:27.891231 00:00 app[web.1]: Command :: identify -format %m '/tmp/97bb878daac24e395c0721c1be8733cd20211025-6-1l11e2y.jpg[0]'
2021-10-25T23:13:27.896884 00:00 app[web.1]: Command :: convert '/tmp/97bb878daac24e395c0721c1be8733cd20211025-6-1l11e2y.jpg[0]' -auto-orient -resize "200x200" '/tmp/4cc67e7e3d2e785af805f6ffc9cc0e8220211025-6-17zwb7y'
2021-10-25T23:13:27.945653 00:00 app[web.1]: Command :: file -b --mime '/tmp/019c51a707e3a0c3ea3263003bcd49ce20211025-6-w4tdp8.jpg'
2021-10-25T23:13:27.951181 00:00 app[web.1]: SQL (0.8ms)  UPDATE "attachments" SET "upload_file_name" = $1, "upload_updated_at" = $2, "processed" = $3, "updated_at" = $4 WHERE "attachments"."id" = $5  [["upload_file_name", "DES_20LOGO_20(512x512).jpg"], ["upload_updated_at", "2021-10-25 23:13:27.870718"], ["processed", "t"], ["updated_at", "2021-10-25 23:13:27.948459"], ["id", 106905]]
2021-10-25T23:13:27.951557 00:00 app[web.1]: [paperclip] saving service/990/attachments/106905-original-DES_20LOGO_20(512x512).jpg
2021-10-25T23:13:27.986837 00:00 app[web.1]: [AWS S3 200 0.034825 0 retries] put_object(:acl=>:private,:bucket_name=>"dpfregen",:content_length=>86383,:content_type=>"image/jpeg",:data=>Paperclip::UriAdapter: DES LOGO%20(512x512).jpg,:key=>"service/990/attachments/106905-original-DES_20LOGO_20(512x512).jpg")
2021-10-25T23:13:27.986839 00:00 app[web.1]:
2021-10-25T23:13:27.986964 00:00 app[web.1]: [paperclip] saving service/990/attachments/106905-medium-DES_20LOGO_20(512x512).jpg
2021-10-25T23:13:28.017915 00:00 app[web.1]: [AWS S3 200 0.029985 0 retries] put_object(:acl=>:private,:bucket_name=>"dpfregen",:content_length=>40823,:content_type=>"image/jpeg",:data=>Paperclip::FileAdapter: 4cc67e7e3d2e785af805f6ffc9cc0e8220211025-6-17zwb7y,:key=>"service/990/attachments/106905-medium-DES_20LOGO_20(512x512).jpg")
2021-10-25T23:13:28.017923 00:00 app[web.1]:
2021-10-25T23:13:28.044967 00:00 app[web.1]: [AWS S3 204 0.026224 0 retries] delete_object(:bucket_name=>"dpfregen",:key=>"uploads/1635203641183-o14kckjjp6d-1311ed2cf3237c252d35885ba8bbd47a/DES LOGO (512x512).jpg")
2021-10-25T23:13:28.044969 00:00 app[web.1]:
2021-10-25T23:13:28.047863 00:00 app[web.1]: SQL (0.8ms)  UPDATE "services" SET "updated_at" = '2021-10-25 23:13:28.045149' WHERE "services"."id" = $1  [["id", 1137]]
2021-10-25T23:13:28.050243 00:00 app[web.1]: SQL (0.6ms)  UPDATE "filters" SET "updated_at" = '2021-10-25 23:13:28.048146' WHERE "filters"."id" = $1  [["id", 990]]
2021-10-25T23:13:28.051630 00:00 app[web.1]: Company Load (0.6ms)  SELECT  "companies".* FROM "companies" WHERE "companies"."id" = $1 LIMIT 1  [["id", 1]]
2021-10-25T23:13:28.054209 00:00 app[web.1]: SQL (0.6ms)  UPDATE "companies" SET "updated_at" = '2021-10-25 23:13:28.052156' WHERE "companies"."id" = $1  [["id", 1]]
2021-10-25T23:13:28.056432 00:00 app[web.1]:  (1.9ms)  COMMIT

:: THEN IT GOES TO THE ENDING SECTION JUST LIKE MY CURRENT APP IS DOING BUT MY CURRENT APP IS SKIPPING ALL OF THE ABOVE ::

2021-10-25T23:13:28.060278 00:00 app[web.1]: Rendered attachments/_attachment.html.haml (2.6ms)
2021-10-25T23:13:28.060371 00:00 app[web.1]: Rendered attachments/create.js.erb (3.0ms)
2021-10-25T23:13:28.060540 00:00 app[web.1]: Completed 200 OK in 312ms (Views: 3.8ms | ActiveRecord: 12.1ms)

Here is a dump of the @service.attachments

@service.attachments: #<ActiveRecord::Associations::CollectionProxy [#<Attachment id: 3391, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/14672362...", upload_file_name: "20160629_141838.jpg", upload_content_type: "image/jpeg", upload_file_size: 664048, upload_updated_at: "2016-06-29 14:37:55.771560000 -0700", processed: true, created_at: "2016-06-29 14:37:55.511793000 -0700", updated_at: "2016-06-29 14:37:55.948425000 -0700", when: "before">, #<Attachment id: 3389, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/14672362...", upload_file_name: "20160629_141808.jpg", upload_content_type: "image/jpeg", upload_file_size: 624308, upload_updated_at: "2016-06-29 14:37:42.278770000 -0700", processed: true, created_at: "2016-06-29 14:37:42.134005000 -0700", updated_at: "2016-06-29 14:37:42.484505000 -0700", when: "before">, #<Attachment id: 3390, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/14672362...", upload_file_name: "20160629_141758.jpg", upload_content_type: "image/jpeg", upload_file_size: 675360, upload_updated_at: "2016-06-29 14:37:53.449659000 -0700", processed: true, created_at: "2016-06-29 14:37:53.068395000 -0700", updated_at: "2016-06-29 14:37:53.671685000 -0700", when: "before">, #<Attachment id: 3437, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/14673121...", upload_file_name: "1467312187074-1193375171.jpg", upload_content_type: "image/jpeg", upload_file_size: 639235, upload_updated_at: "2016-06-30 11:42:58.287106000 -0700", processed: true, created_at: "2016-06-30 11:42:57.996008000 -0700", updated_at: "2016-06-30 11:42:59.051767000 -0700", when: "after">, #<Attachment id: 3439, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/14673122...", upload_file_name: "1467312251285-1977276610.jpg", upload_content_type: "image/jpeg", upload_file_size: 877201, upload_updated_at: "2016-06-30 11:44:01.036497000 -0700", processed: true, created_at: "2016-06-30 11:44:00.642325000 -0700", updated_at: "2016-06-30 11:44:01.653891000 -0700", when: "after">, 
:: THIS BELOW IS THE FILE THAT WAS JUST UPLOADED, NOTICE THE attachment_id is NILL, this is because it hasn't been saved ::
#<Attachment id: nil, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/16365640...", upload_file_name: nil, upload_content_type: nil, upload_file_size: nil, upload_updated_at: nil, processed: false, created_at: nil, updated_at: nil, when: nil>]>

Here is the @attachment at the same point in time:

@attachment: #<Attachment id: nil, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/16365640...", upload_file_name: nil, upload_content_type: nil, upload_file_size: nil, upload_updated_at: nil, processed: false, created_at: nil, updated_at: nil, when: nil>

Here is a dump of the params at the same time:

params#<ActionController::Parameters {"url"=>"https://mybucket.s3.amazonaws.com/uploads/1636564061858-fi8lg02378-65280638d6d13515e74449339d1aa926/DES LOGO (512x512).jpg", "filepath"=>"/uploads/1636564061858-fi8lg02378-65280638d6d13515e74449339d1aa926/DES LOGO (512x512).jpg", "filename"=>"DES LOGO (512x512).jpg", "filesize"=>"86383", "lastModifiedDate"=>"Wed May 24 2017 11:55:08 GMT-0700 (Pacific Daylight Time)", "filetype"=>"image/jpeg", "unique_id"=>"fi8lg02378", "attachment"=>#<ActionController::Parameters {"direct_upload_url"=>"https://mybucket.s3.amazonaws.com/uploads/1636564061858-fi8lg02378-65280638d6d13515e74449339d1aa926/DES LOGO (512x512).jpg"} permitted: true>, "when"=>"before", "controller"=>"attachments", "action"=>"create", "customer_id"=>"419", "filter_id"=>"990", "service_id"=>"1137"} permitted: true>

If I inspect @attachment.save I get this:

@attachment.save:false

As far as I can tell the reason the second process called :queue_processing which is on an after_create callback isn't starting because the @attachement.save isn't SAVING. WHY it isn't saving I do not understand. I have tried all kinds of manual ways to try to FORCE the save but it doesn't work, I tried adding @attachment = Attachment.save, I tried adding @attachment = Attachment.create(attachment_params), I tried adding @attachment = Attachment.create(attachment_params[:id]) all before the @attachment.save and it will NOT save no matter what I do, I just don't understand WHY it will not save. The same code on the older version of rails does save and it works at that point.

If anyone has ANY idea I would LOVE to hear then and would be VERY grateful. I am assuming the problem is coming from the @attachment.save but I suppose it could also be that the s3_direct_upload is not making the callback correctly but I don't know how to troubleshoot that part. I did at console.log statements in the js file and it does get to the correct place and it does trigger the s3_direct_upload process as far as I can tell as I put all kinds of console.log entries in it at each step and each step is working properly best I can tell.

I also tried upgrading to to the later version of aws-sdk (version 3) and the kt-paperclip and it does the same exact thing so It doesn't appear to be a failure of paperclip or aws best I can tell. I am even open to figuring out how I can grab the next ID from the DB manually and then calling the queue_proccessing manually after the @attachment.save (which I have tried to do). The only reason I can't get that to work is because the @attachment.save isn't saving so it isn't returning an ID and without the ID I can't manually call it.

Any help would be VERY Appreciated. I am even willing to hire or pay someone for help with this as I am very stuck and need to understand the answer to this. I have a second app that is failing the very same way in the very same location. This other app is also a re-write from an even older version of Rails v3.2 to current. This is the ONLY part I have been unable to fix.

If anyone wants me to post anything else let me know and I will be more than happy to post whatever else anyone things would help.

Thank You, Scott

CodePudding user response:

On @attachment.save try @attachment.valid? to check if object is valid or not If the object is invalid then check the error messages by @attachment.errors.full_messages You can clearly see the missing and invalid attributes

Here " @attachment: #<Attachment id: nil, service_id: 1137, user_id: nil, direct_upload_url: "https://mybucket.s3.amazonaws.com/uploads/16365640...", upload_file_name: nil, upload_content_type: nil, upload_file_size: nil, upload_updated_at: nil, processed: false, created_at: nil, updated_at: nil, when: nil>" You can see the user_id is missing set user id then try again i hope after this your problem get resolve

  • Related