百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

carrierwave-mongoid

> 编程语言
开源

Mongoid 对 CarrierWave 的支持

353 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Mongoid 对 CarrierWave 的支持

CarrierWave for Mongoid

This gem adds support for Mongoid and MongoDB's GridFS to CarrierWave

This functionality used to be part of CarrierWave but has since been extracted into this gem.

Installation

Install the latest release:

gem install carrierwave-mongoid

Require it in your code:

ruby
require 'carrierwave/mongoid'

Or, in Rails you can add it to your Gemfile:

ruby
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'

Note: If using Rails 4, you'll need to make sure mongoid-grid_fs is >= 1.9.0. If in doubt, run bundle update mongoid-grid_fs

ruby
gem 'mongoid-grid_fs', github: 'ahoward/mongoid-grid_fs'

Getting Started

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:

ruby
u = User.new
u.avatar = File.open('somewhere')
u.save!

Using MongoDB's GridFS store

In your uploader, set the storage to :grid_fs:

ruby
class AvatarUploader  'gridfs#avatar'

Now, user.avatar.url should return an appropriate url path to use in your views.

Different uploaded versions

If you need to include different versions (e.g. thumbnails), additional routes will help:

ruby
match '/uploads/grid/user/avatar/:id/:filename' => 'gridfs#thumb_avatar', constraints: { filename: /thumb.*/ }

Version differences

Version Notes
~> 1.5.0 ([compare][compare-1.5], [dependencies][deps-1.5]) Mongoid 9.0 support
~> 1.4.0 ([compare][compare-1.4], [dependencies][deps-1.4]) Mongoid 8.0 support
~> 1.3.0 ([compare][compare-1.3], [dependencies][deps-1.3]) CarrierWave 2.0 support
~> 1.2.0 ([compare][compare-1.2], [dependencies][deps-1.2]) Carrierwave 1.3 support
~> 1.1.0 ([compare][compare-1.1], [dependencies][deps-1.1]) Mongoid 7.0 support
~> 1.0.0 ([compare][compare-1.0], [dependencies][deps-1.0]) Carrierwave 1.x support
~> 0.10.0 ([compare][compare-0.10], [dependencies][deps-0.10]) Mongoid 6.0 support
~> 0.9.0 ([compare][compare-0.9], [dependencies][deps-0.9]) Carrierwave 0.11 support
~> 0.8.0 ([compare][compare-0.8], [dependencies][deps-0.8]) Mongoid 5 support, bug fixes
~> 0.7.0 ([compare][compare-0.7], [dependencies][deps-0.7]) Mongoid 3 & 4, bug fixes
~> 0.6.0 ([compare][compare-0.6], [dependencies][deps-0.6]) Mongoid 3 & 4, bug fixes
~> 0.5.0 ([compare][compare-0.5], [dependencies][deps-0.5]) Mongoid::Paranoia support
~> 0.4.0 ([compare][compare-0.4], [dependencies][deps-0.4]) Carrierwave bump
~> 0.3.0 ([compare][compare-0.3], [dependencies][deps-0.3]) Mongoid >= 3.0
~> 0.2.0 ([compare][compare-0.2], [dependencies][deps-0.2]) Rails >= 3.2, Mongoid ~> 2.0
~> 0.1.0 ([compare][compare-0.1], [dependencies][deps-0.1]) Rails 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:

ruby
class Dokument
  mount_uploader :upload, DokumentUploader, mount_on: :upload_filename
end

Known issues and limitations

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:

ruby
class User
  embeds_many :pictures, cascade_callbacks: true
end

You can read more about this here

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言