CarrierWave for Mongoid
This gem adds support for Mongoid and MongoDB's GridFS to CarrierWave, see the CarrierWave documentation for more detailed usage instructions.
This version supports ONLY version of mongoid ~> 2.1
Keep in mind that if you came up from previous versions you should make a few steps to go with it:
- change(rename in db) field name from
avatar(appropriate to your uploader name)
- fix you code where you need to operate with uploaded file's filename from
Install it like this:
gem install carrierwave-mongoid
Use it like this:
Make sure to disable auto_validation on the mounted column.
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
This used to be part of CarrierWave but has been extracted.
Using MongoDB's GridFS store
You'll need to configure the database and host to use:
CarrierWave.configure do |config| config.grid_fs_database = 'my_mongo_database' config.grid_fs_host = 'mongo.example.com' end
The defaults are
And then in your uploader, set the storage to
class AvatarUploader < CarrierWave::Uploader::Base storage :grid_fs end
Since GridFS doesn't make the files available via HTTP, you'll need to stream
them yourself. In Rails for example, you could use the
send_data method. You
can tell CarrierWave the URL you will serve your images from, allowing it to
generate the correct URL, by setting eg:
CarrierWave.configure do |config| config.grid_fs_access_url = "/image/show" end
Known issues/ limitations
If using Mongoid, note that embedded documents files aren't saved when parent documents are saved. You must explicitly call save on embedded documents in order to save their attached files. You can read more about this here