开发者

The best solution to keep files in database (Rails)

开发者 https://www.devze.com 2023-01-19 01:59 出处:网络
I would like to know what will be the best way to save binary files \'in database\'. Of course they wi开发者_如何学JAVAll be on the disk as files, but i need some \'link\' to them in the DB.

I would like to know what will be the best way to save binary files 'in database'. Of course they wi开发者_如何学JAVAll be on the disk as files, but i need some 'link' to them in the DB. Any great solutions?


Use Paperclip to attach a file to a model.

Say you have a mortgage that has a document

class Mortgage < ActiveRecord::Base
  has_attached_file :document
end

Later:

mortgage = Mortgage.find(params[:id])
document = mortgage.document

Paperclip is usually used with images, but works with all types of files. You can easily store on s3 as well.


If you don't have anything against mongodb and gridfs, there's an example here http://socialmemorycomplex.net/2010/06/02/gridfs-with-mongoid-and-carrierwave-on-rails-3/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号