Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 621
mlzboy Avatar asked Jan 13 '11 12:01

mlzboy


People also ask

How to store Image in Rails database?

Seeding Images in Your Rails Database To do so, you can use the 'attach' method, passing in a hash with two keys. The 'io' key calls the open method on the File class and takes the image's relative path as an argument. article. save!

How do you use a paperclip gem in rails?

Setting Up PaperclipTo set up Paperclip, first we need to install the ImageMagick dependency. Paperclip uses ImageMagick to resize images after upload. If you are using another system, you can get download and install instructions from the ImageMagick website. Run bundle install to finish it up.


1 Answers

To further clarify @andrea's answer:

YourPaperclippedModelHere.new(:your_paperclip_field => File.new(path, "r"))

So if your model is called Image and your paperclip field is data:

Image.new(:data => File.new(path_to_your_file, "r"))

like image 115
ZiggyTheHamster Avatar answered Oct 05 '22 20:10

ZiggyTheHamster