将回形针与现有列一起使用

时间:2015-02-02 09:49:15

标签: ruby-on-rails ruby paperclip

我有一个名为" profile_picture"的现有数据库列。这是目前的文本专栏。我想使用paperclip gem来允许用户上传他们自己的" profile_picture"。

我遇到这个问题的最佳方式是什么?我应该删除" profile_picture"列并运行新的回形针迁移,还是可以以某种方式修改此列并添加关联的file_name,content_type等字段?

1 个答案:

答案 0 :(得分:0)

由于procile_picture不是回形针通常使用的列(属性profile_picture使用列profile_picture_file_name),因此无需删除列。但是,如果您的数据库中的列没有映射到类似命名的属性(因为set和get方法由paperclip提供),那么我很困惑,因此我可能会在生成回形针之前将其重命名...或者更好:如果没用,只需删除该列。尽可能地整理一下。

我强烈建议不要将列重命名为profile_picture_file_name,因为它可能包含回形针无法处理的数据。

class AddAvatarColumnsToUsers < ActiveRecord::Migration
  def self.up
    rename_column :users, :profile_picture, :old_profile_picture
    add_attachment :users, :profile_picture
  end
  def self.down
    remove_attachment :users, :profile_picture
    rename_column :users,  :old_profile_picture, :profile_picture
  end
end