【问题标题】:Changing my model's ID to a UUID corrupted Paperclip file paths将我的模型 ID 更改为 UUID 损坏的回形针文件路径
【发布时间】:2016-11-19 22:09:56
【问题描述】:

我有一个名为 SpacePhoto 的模型。 SpacePhoto has_attached_file :photo 通过回形针。

SpacePhoto 曾经有一个整数 ID,但我最近将其更改为 UUID,并进行了以下迁移:

class ChangePhotoPrimaryKeyType < ActiveRecord::Migration
  def change
    add_column :space_photos, :uuid, :uuid, default: "uuid_generate_v4()", null: false

    change_table :space_photos do |t|
      t.remove :id
      t.rename :uuid, :id
    end

    execute "ALTER TABLE space_photos ADD PRIMARY KEY (id);"
  end
end

要获取 url,我会使用space_photo.photo.url,但现在它不再给我正确的 url。例如,在运行迁移之前,我会得到 url https://s3-us-west-2.amazonaws.com/instally.beta/space_photos/photos/000/000/071/original/avatar.jpg?1478889772 。现在,我得到https://s3-us-west-2.amazonaws.com/instally.beta/space_photos/photos/086/a71/a9-/original/avatar.jpg?1479167261

区别在于 URL 中间的 3 组 3 个字符,但我对 Paperclip 的内部结构了解得不够多,无法弄清楚这些字符的来源。

【问题讨论】:

    标签: ruby-on-rails amazon-web-services amazon-s3 paperclip


    【解决方案1】:

    您遇到的问题是使用 :id_partition 插值的附件的回形针 URL 的结果,该插值将 id 拆分为前 9 个字符的 3 个字符拆分路径形式。

    我怀疑这是因为您没有提供 url 选项,而 Paperclip 使用的是默认值:

    /system/:class/:attachment/:id_partition/:style/:filename
    

    要覆盖它,您需要指定 url 选项并将 :id_partition 替换为 :id 插值。在具有附件的模型中,将 has_attached_file 声明更改为:

    has_attached_file :photo,
                        url: '/system/:class/:attachment/:id/:style/:filename'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多