Alex Rivera | Logout

Paperclip: How to store a picture in a Rails console?

Asked 2011-01-13T12:52:57.527
38

I tried storing a local image in a rails console.

Because I have many pictures in my local storage (I use crawler to download tons of pictures), I want to store them into a database, with the benefit of paperclip to do some image job, like thumbnail etc. If I use a webpage to save new pictures to database one by one, it will cost a lot of time. So I want to find a way in rails console (some code) that can batch save-picture-into-database.

Edit
Report

1 Answer

19

If this is the model:

class User < ActiveRecord::Base
  has_attached_file :avatar
end

then the following should work from the console:

>> User.create(:avatar => File.open('/path/to/image.jpg', 'rb'))
answered 2011-02-04T17:55:39.700

Your Answer