开发者

Good solution for tagging in Rails with MongoID [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 6 years ago.

Improve this que开发者_如何学Cstion

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?


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.


Try mongoid_taggable gem. It does want you are looking for.


The mongoid-tags-arent-hard gem seems more capable (and more actively maintained) than the other gems I've seen.


here it is a "scalable" solution for map/reduce and real time aggregation strategies mongoid_taggable_with_context

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜