Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

54 lines (33 sloc) 1.616 kb

CarrierWave for Mongoid

This gem adds support for Mongoid and MongoDB's GridFS to CarrierWave, see the CarrierWave documentation for more detailed usage instructions.

Install it like this:

gem install carrierwave-mongoid

Use it like this:

require 'carrierwave/mongoid'

Make sure to disable auto_validation on the mounted column.

Using bundler:

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 'carrierwave' and 'localhost'.

And then in your uploader, set the storage to :grid_fs:

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

Jump to Line
Something went wrong with that request. Please try again.