我正在創建一個將執行以下三件事的遷移:創建一個名爲images
的表,將products
表中的所有圖像轉移到新的images
表中,然後從products
表中刪除所有圖像列。如何創建將所有圖像從一個表格轉移到另一個表格的Rails遷移?
除傳輸圖像部分外,一切正常。沒有圖像信息傳輸。
這裏的遷移:
class CreateImages < ActiveRecord::Migration
def change
create_table :images do |t|
t.belongs_to :product
t.string :image_file_name, :image_content_type
t.integer :image_file_size
t.boolean :main, :default => false
t.timestamps
end
Product.all.each do |product|
begin
product.images.create(:image => product.image, :main => true)
rescue => e
logger.warn "Error while transferring images from product: #{product.name} to Images: #{e}"
end
end
remove_column :products, :image_file_name
remove_column :products, :image_content_type
remove_column :products, :image_file_size
remove_column :products, :image_updated_at
add_index :images, [:product_id, :main]
end
end
謝謝你的幫助提示。你知道我應該如何傳送圖像嗎? – Snubber
剛剛更新了我的答案。 –