Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Mongoid taggable behaviour

branch: master

Fetching latest commit…


Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 rails
Octocat-spinner-32 spec
Octocat-spinner-32 tasks
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.textile
Octocat-spinner-32 Rakefile
Octocat-spinner-32 init.rb
Octocat-spinner-32 install.rb
Octocat-spinner-32 mongoid_taggable.gemspec
Octocat-spinner-32 uninstall.rb

Mongoid Taggable

Mongoid Taggable provides some helpers to create taggable documents.


You can simple install from rubygems:

gem install mongoid_taggable

or as a Rails Plugin:

script/plugin install git://

Basic Usage

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

class Post
  include Mongoid::Document
  include Mongoid::Taggable
  field :title
  field :content

Them in your form:

<% form_for @post do |f| %>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
    <%= f.label :content %><br />
    <%= f.text_area :content %>
    <%= f.label :tags %><br />
    <%= f.text_field :tags %>
    <button type="submit">Send</button>
<% end %>

In this case, the text fields for tags should receive the list of tags separated by colon (above in this document you will see how to change the separator)

Them your document will have the tags and tags_array getter and setter. The tags you use as a plain string with tags separated by colon, the tags_array is an array with tags, these two properties are automatic synchronized.

Tags Indexing

This lib will automatic create for you and index of tags after save the document, this index is useful to get a list of all tags used in documents of this collection or to create a tag cloud. See the following example to understand how to use it:

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]
# ]

If you don’t want to use this feature, its good to disable for improve performance:

class Post
  include Mongoid::Document
  include Mongoid::Taggable
  disable_tags_index! # will disable index creation
  field :title
  field :content

Changing default separator

To change default separator you just need to call tags_separator method on class:

class Post
  include Mongoid::Document
  include Mongoid::Taggable
  tags_separator ';' # will change tags separator to ;
  field :title
  field :content
Something went wrong with that request. Please try again.