Paperclipでadd_attachmentする時にafterを指定したい
これを実行すると
class AddAttachmentToUsers < ActiveRecord::Migration def change add_attachment :users, :avatar, after: 'name' end end
usersテーブルに下の列が追加されてしまいます。
- avater_file_name
- avater_content_type
- avater_file_size
- avater_updated_at
- {:after=>"name"}_file_name
- {:after=>"name"}_content_type
- {:after=>"name"}_file_size
- {:after=>"name"}_updated_at
add_attachmentの実装を見てみると、こんな感じ。
# gems/paperclip-3.5.2/lib/generators/paperclip/paperclip_generator.rb module Paperclip module Schema COLUMNS = {:file_name => :string, :content_type => :string, :file_size => :integer, :updated_at => :datetime} # 間省略 module Statements def add_attachment(table_name, *attachment_names) raise ArgumentError, "Please specify attachment name in your add_attachment call in your migration." if attachment_names.empty? attachment_names.each do |attachment_name| COLUMNS.each_pair do |column_name, column_type| add_column(table_name, "#{attachment_name}_#{column_name}", column_type) end end end # 間省略 end end end
オプションを指定することができないので、仕方なくこうやりました。
class AddAvaterToUsers < ActiveRecord::Migration def change Hash[*Paperclip::Schema::COLUMNS.to_a.reverse.flatten].each_pair do |column_name, column_type| add_column(:users, "avater_#{column_name}", column_type, after: 'name') end end end
カラムのhashの定義は直接書いてしまってもよかったかも。