My Octopress Blog

A blogging framework for hackers.

Using Attachment_fu From Script Console

I recently had to migrate a whole bunch of photos from one app to another, and since I’m using AttachmentFu for the image attachments I just opened up script/console so do the dirty work.

However, the dirty work didn’t work.

With FileColumn, I could just go object.filename = “directory/#{filename}”, but that didn’t seem to do the trick with AttachmentFu. I needed to provide size and content_type.

I added an extension to the File class called content_type:

class File
  # your expected mime types here
    'jpg'  => 'image/jpeg',
    'png' => 'image/png',
    'gif' => 'image/gif'
  def content_type
    FILE_EXTENSION_MIME_TYPES[File.extname(self.path).downcase.gsub(/^\./,'')] || 'application/octet-stream'

and then had to use this call to create the new attachment_fu object:

f =

   :temp_data =>,
   :content_type => f.content_type,
   :filename => file.split('/').last

and now in action:

Property.find(:all).each do |p|
  next if > 0
  default = true
  Dir.glob("ap/#{p.internal_code}/*").each do |f|
    file =
    PropertyPhoto.create(:property_id =>, :temp_data =>, :content_type => file.content_type, :filename => f.split('/').last, :default => default)
    default = false

Hope that helps a few of you figure it out faster than I did.