This functionality used to be part of CarrierWave but has since been extracted into this gem.
Install the latest release:
gem install carrierwave-mongoid
Require it in your code:
require 'carrierwave/mongoid'
Or, in Rails you can add it to your Gemfile:
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
Follow the "Getting Started" directions in the main Carrierwave repository.
Now you can cache files by assigning them to the attribute; they will automatically be stored when the record is saved. Ex:
u = User.new
u.avatar = File.open('somewhere')
u.save!
Version | Notes |
---|---|
~> 1.1.0 | (compare, dependencies) Mongoid 7.0 support |
~> 1.0.0 | (compare, dependencies) Carrierwave 1.x support |
~> 0.10.0 | (compare, dependencies) Mongoid 6.0 support |
~> 0.9.0 | (compare, dependencies) Carrierwave 0.11 support |
~> 0.8.0 | (compare, dependencies) Mongoid 5 support, bug fixes |
~> 0.7.0 | (compare, dependencies) Mongoid 3 & 4, bug fixes |
~> 0.6.0 | (compare, dependencies) Mongoid 3 & 4, bug fixes |
~> 0.5.0 | (compare, dependencies) Mongoid::Paranoia support |
~> 0.4.0 | (compare, dependencies) Carrierwave bump |
~> 0.3.0 | (compare, dependencies) Mongoid >= 3.0 |
~> 0.2.0 | (compare, dependencies) Rails >= 3.2, Mongoid ~> 2.0 |
~> 0.1.0 | (compare, dependencies) Rails <= 3.1 |
CarrierWave used to have built-in Mongoid support. This gem replaces that support and only supports Mongoid ~> 2.1
You can use upload_identifier
to retrieve the original name of the uploaded file.
In the earlier version, the mount_uploader-method for mongoid had been defined in lib/carrierwave/orm/mongoid. This code has been moved to carrierwave/mongoid. If you update from earlier versions, don't forget to adjust your require accordingly in your carrierwave-initializer.
The default mount column used to be the name of the upload column plus
_filename
. Now it is simply the name of the column. Most of the time, the
column was called upload
, so it would have been mounted to upload_filename
.
If you'd like to avoid a database migration, simply use the :mount_on
option
to specify the field name explicitly. Therefore, you only have to add a
_filename
to your column name. For example, if your column is called
:upload
:
class Dokument
mount_uploader :upload, DokumentUploader, mount_on: :upload_filename
end
Note that files mounted in embedded documents aren't saved when parent documents are saved. By default, mongoid does not cascade callbacks on embedded documents. In order to save the attached files on embedded documents, you must either explicitly call save on the embedded documents or you must configure the embedded association to cascade the callbacks automatically. For example:
class User
embeds_many :pictures, cascade_callbacks: true
end
You can read more about this here