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

mongoid_taggable

> 编程语言
开源

Mongoid 可标记行为

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

工具介绍

Mongoid 可标记行为

h1. Mongoid Taggable

Mongoid Taggable provides some helpers to create taggable documents.

h2. Installation

You can install from rubygems:

bc. gem install mongoid_taggable

or in Gemfile:

bc. gem 'mongoid_taggable'

or as a Rails Plugin:

bc. script/plugin install git://github.com/wilkerlucio/mongoid_taggable.git

h2. Mongoid 2 vs Mongoid 3

All gem versions less than 1.x only support Mongoid 2 while versions 1.x and greater support Mongoid 3.

h2. Basic Usage

To make a document taggable you need to include @Mongoid::Taggable@ into your document:

bc.. class Post include Mongoid::Document include Mongoid::Taggable

field :title field :content end

p. And in your form:

bc..

Send

p. In this case, the text fields for tags should receive the list of tags separated by comma (See below for how to change the separator).

p. Then your document will have the @tags@ and @tags_array@ getter and setter. @tags@ is as a plain string with tags separated by comma, and @tags_array@ an array representation.

h2. Finding Objects by Tag

p. Tagged models get a scope called @tagged_with@, @tagged_with_all@, and @tagged_with_any@:

bc.. MyModel.tagged_with('foo') MyModel.published.tagged_with('foo').count MyModel.tagged_with_all('foo', 'bar') MyModel.tagged_with_all(['foo', 'bar']) MyModel.tagged_with_any('foo', 'bar') MyModel.tagged_with_any(['foo', 'bar'])

h2. Tags Indexing

This module will automatically create an index of tags and their counts for you after saving the document. This can be used for a tag cloud. See the following example to understand how to use it:

bc.. Post.create!(:tags => "food,ant,bee") Post.create!(:tags => "juice,food,bee,zip") Post.create!(:tags => "honey,strip,food")

Post.tags # will retrieve ["ant", "bee", "food", "honey", "juice", "strip", "zip"] Post.tags_with_weight # will retrieve:

[

['ant', 1],

['bee', 2],

['food', 3],

['honey', 1],

['juice', 1],

['strip', 1],

['zip', 1]

]

p. The tags index is unaffected by default_scopes. If enabled, all tags will be indexed regardless of other attribute values. p. If you don't want to use this feature, it is good to disable it to improve performance:

bc.. class Post include Mongoid::Document include Mongoid::Taggable

disable_tags_index! # will disable index creation

field :title field :content end

h2. Changing default separator

To change the default separator you may call the @tags_separator@ class method:

bc.. class Post include Mongoid::Document include Mongoid::Taggable

tags_separator ';' # will change tags separator to ;

field :title field :content end

Issues· 0 开放

查看全部 Issues在 GitHub 打开

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

> 标签

Ruby

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

> 工具信息

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

> 相关工具

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