Tuesday, March 30, 2010

Adding tags to Mongoid Documents

In our latest project at Martian Interactive we are using Mongoid to support some document-based models instead of a relational approach like ActiveRecord models. As many applications, this one needed a tagging system (nothing magic, we all know there are a lot of gems/plugins out there that works pretty fine with AR) for Mongoid documents, so I ended up writting a really basic and simple tagging system that's working pretty well, since we are using a document-based schema each document has it's own set of embedded tags that are loaded when the document is required allowing us to save some queries, with a relational schema this solution wouldn't be an option due to the redundance it introduces.

Here's the code.

9 comments:

Kyle said...
This comment has been removed by the author.
Kyle said...

Great Post, this is exactly what I needed. Just in case someone else uses this runs into an error, the tags method is missing the closing tag. Add that and you will be good to go.

jpemberthy said...

ah! yes good catch, thanks!

Fernando said...

Newbie question.

Where do I need to put this file and with what name?

jpemberthy said...

@fernando You can save that file under /lib/mongoid_tags.rb and then in your environment.rb(rails 2.3.x) or application.rb(rails 3) you can load the lib.

http://stackoverflow.com/questions/3356742/best-way-to-load-module-class-from-lib-folder-in-rails-3

kadoudal said...

Thanks a lot ... that's what I was looking for .. a simple tagging function ....

Peter said...

Pc games free
gta 4 free download

atom said...

nice buddy
__________
New pc games

all games full free

Tigraine said...

Great job.. saved me a bunch of work..

Updated for rails 3.2 with it's mass assignment protection.
Also the mongoid index method changed a bit:
https://gist.github.com/3313759