Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good solution for tagging in Rails with MongoID [closed]

What are good solutions for tagging in Rails with MongoID?

It seems that it is really simple to just add a hash or array to a document, but I am not sure if that is the best approach.

Maybe some Gem? Or a simple trick with nested documents?

like image 663
berkes Avatar asked Mar 28 '11 10:03

berkes


1 Answers

For now, I used a very simple approach, that works very well: Just include an Array-field.

#app/models/image.rb
class Image
  include Mongoid::Document
  include Mongoid::Timestamps

  field :message, :type => String
  field :tags, :type => Array

  def self.images_for tag
    Image.any_in(:tags => [tag])
  end
end

#routes.rb
match "tag/:tag" => "images#tag"

#/app/controller/images_controller.rb
class ImagesController < ApplicationController
  # GET /tag
  # GET /tag.xml
  def tag
    @images = Image.images_for params[:tag]

    respond_to do |format|
      format.html { render "index" }
      format.xml  { render :xml => @images }
    end
  end
end

This works, but I am a still a little doubtfull about the performance of the Image.any_in map/reduce. I think there may be a better solution for that map/reduce but have not found it, yet.

like image 50
berkes Avatar answered Sep 28 '22 07:09

berkes