Переход Carrierwave Base64 на другую модель - PullRequest
0 голосов
/ 10 апреля 2020

Я пытаюсь выполнить миграцию, чтобы упростить нашу базу данных, но по какой-то причине она не переносит загрузки Carrierwave в новую модель. Это модели:

class GalleryImage < ApplicationRecord
  include RichText
  include Taggable
  include Queueable

  # Include gallery image helper methods
  include Image

  belongs_to :user
  belongs_to :folder
  belongs_to :thumbnail, :class_name => "PlainImage", foreign_key: :thumbnail_id

  mount_base64_uploader :image, GalleryImageUploader

  # ... more code
end
class PostAttachment < ApplicationRecord
  # Include gallery image helper methods
  #include Image

  belongs_to :post
  belongs_to :user

  mount_base64_uploader :file, PostAttachmentUploader

  validates :post, :user, :file, presence: true
  #validates :file, file_size: { less_than: @@file_size_limit }
  validate :must_be_same_uploader

  #after_create :post_process_upload

  def must_be_same_uploader
    if post.user.id != user.id
      errors.add(:error, "Unathorized")
    end
  end
end

и моя миграция здесь:

class RefactorGalleryToPosts < ActiveRecord::Migration[6.0]
  def up
    # Rename GalleryImages to Posts
    rename_table :gallery_images, :posts
    add_column :posts, :post_type, :integer # [:text, :image, ... etc]
    rename_column :posts, :nsfw, :adult # For consistency

    # Rename PlainImages to Files
    rename_table :plain_images, :post_attachments
    rename_column :post_attachments, :gallery_image_id, :post_id
    rename_column :post_attachments, :image, :file
    remove_column :post_attachments, :user_id
    remove_column :post_attachments, :content_type
    remove_column :post_attachments, :width
    remove_column :post_attachments, :height

    say_with_time "migrate existing data" do
      Post.all.update_all(post_type: 1)
      Post.all.each_with_index do |post, i|
        say "#{i}/#{Post.all.count} - #{(i.to_f / Post.all.count.to_f * 100.0).round(2)}%"

        postAttachment = PostAttachment.new(
          post_id: post.id,
          file: post.image, # TODO: FIX LINE, NOT WORKING
        )
        postAttachment.save(:validate => false)
      end
    end

    # Remove obsolete columns
    remove_column :posts, :image
    remove_column :posts, :rendered_description
    remove_column :posts, :width
    remove_column :posts, :height
    remove_column :posts, :status
    remove_column :posts, :job_id
  end
end

Я бы предположил, что, поскольку я имею дело с загрузчиком base64, простая передача значений должна быть достаточным. К сожалению, мои PostAttachments в конечном итоге имеют нулевое значение.

...