Commit 28d84d70

mo k <mo@mokhan.ca>
2012-11-08 03:39:50
convert photo migration method to a Custom Job class.
1 parent c4cf2d0
app/models/creation.rb
@@ -28,14 +28,6 @@ class Creation < ActiveRecord::Base
     end
   end
 
-  def migrate_primary_image
-      photo = photos.build({})
-      photo.created_at = created_at
-      photo.updated_at = updated_at
-      photo.image = image.file
-      photo.save!
-  end
-
   def published?
     photos.count > 0
   end
db/migrate/20121010190225_migrate_images_for_each_creation.rb
@@ -1,9 +1,19 @@
+class MigratePrimaryImage < Struct.new(:creation_id)
+  def perform
+    creation = Creation.find(creation_id)
+    photo = creation.photos.build({})
+    photo.created_at = creation.created_at
+    photo.updated_at = creation.updated_at
+    photo.image = creation.image.file
+    photo.save!
+  end
+end
+
 class MigrateImagesForEachCreation < ActiveRecord::Migration
   def up
     Creation.all.each_with_index do |creation, index|
-      creation.delay.migrate_primary_image
+      Delayed::Job.enqueue MigratePrimaryImage.new(creation.id)
     end
-    #remove_column :creations, :image
   end
 
   def down