Jeriko
Jeriko

Reputation: 6637

Rails / Paperclip attaching via command line

I have a bunch of jpeg files in a folder on my server, and I'm attempting to attach them to their corresponding Property instances through a rake task.

property.rb has the following code:

  has_attached_file :temp_photo,
    :styles => PropertyImage::STYLES,
    :url => "/assets/:class/:attachment/:id_partition/:style_:basename.:extension",
    :path => "#{Rails.root}/public/assets/:class/:attachment/:id_partition/:style_:basename.:extension"

I use paperclip on other models, and there are no issues whatsoever, but I get a problem when I attempt the following:

p = Property.find(id)
file = File.open(temp_file_path)
p.temp_photo = file
p.save

# => false

file.close
p.errors

# => "/tmp/stream20110524-1126-1cunv0y-0.jpg is not recognized by the 'identify' command."

The file definitely exists, and I've tried changing the permissions. Restarting the server doesn't help. The problem seems to be with using the command line, as the normal form / HTTP approach works fine. This is only a temporary set-up, so I'm looking for a working way to import a batch of files into my rails app paperclip model.

Any suggestions?

Upvotes: 3

Views: 7217

Answers (1)

ukitazume
ukitazume

Reputation: 51

path = 'target_file_path'
attach_name = 'temp_photo'

p = Property.find(id)
attach = Paperclip::Attachment.new(attach_name, p, p.class.attachment_definitions[attach_name.to_suym])

file = File.open(path) 
attach.assign file
attach.save

file.close

Upvotes: 5

Related Questions