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

mongoid-paperclip

> 编程语言
开源

Mongoid::Paperclip 允许您将 Paperclip 用于 MongoDB 的 Mongoid ODM。

344 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

Mongoid::Paperclip 允许您将 Paperclip 用于 MongoDB 的 Mongoid ODM。

Mongoid::Paperclip

Integrate kt-paperclip into Mongoid. (Kt-paperclip is a maintained fork of the original Paperclip that is now deprecated)

This is actually easier and faster to set up than when using Paperclip and the ActiveRecord ORM.

Setting it up

Gemfile

rb
gem "mongoid-paperclip"

Next let's assume we have a User model and we want to allow our users to upload an avatar.

Rails.root/app/models/user.rb - include the Mongoid::Paperclip module and invoke the provided class method

rb
class User
  include Mongoid::Document
  include Mongoid::Paperclip

  has_mongoid_attached_file :avatar
end

That's it

That's all you have to do. Users can now upload avatars. Unlike ActiveRecord, Mongoid doesn't use migrations, so we don't need to define the Paperclip columns in a separate file. Invoking has_mongoid_attached_file will automatically define the necessary :avatar fields for you.

A more complex example

Just like Paperclip, Mongoid::Paperclip takes a second argument (hash of options) for the has_mongoid_attached_file method, so you can do more complex things such as in the following example.

…

Note on embedded documents: if you plan to save or update the parent document, you MUST add cascade_callbacks: true to your embeds_XXX statement. Otherwise, your data will be updated but the paperclip functions will not run to copy/update your file.

In the above example:

ruby
class User
  embeds_many :pictures, :cascade_callbacks => true
  accepts_nested_attributes_for :pictures, ...
  attr_accepted :pictures_attributes, ...
end

@user.update_attributes({ ... :pictures => [...] })

Optional fingerprinting

Paperclip will skip calculating the fingerprint of a file when the {file}_fingerprint field is missing from the model. This can be desirable if attaching large files to a model. To disable adding the fingerprint field pass the disable_fingerprint option as in this example:

rb
class User
  include Mongoid::Document
  include Mongoid::Paperclip

  has_mongoid_attached_file :usage_report, disable_fingerprint: true
end

Testing

If you want to help develop this plugin, clone the repo and bundle to get all dependencies.

Then to run the tests:

rspec

There you go

Quite a lot of people have been looking for a solution to use Paperclip with Mongoid so I hope this helps!

If you need more information on either Mongoid or kt-paperclip I suggest checking our their official documentation and website.

License

Mongoid::Paperclip is released under the MIT license. See LICENSE for more information.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

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

> 标签

Ruby

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

> 工具信息

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

> 相关工具

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