【问题标题】:Good solution for tagging in Rails with MongoID [closed]使用 MongoID 在 Rails 中进行标记的良好解决方案 [关闭]
【发布时间】:2011-03-28 10:44:15
【问题描述】:

在 Rails 中使用 MongoID 进行标记有什么好的解决方案?

似乎向文档中添加哈希或数组非常简单,但我不确定这是否是最好的方法。

也许是一些宝石?还是嵌套文档的简单技巧?

【问题讨论】:

    标签: ruby-on-rails-3 mongodb mongoid tagging


    【解决方案1】:

    目前,我使用了一种非常简单的方法,效果很好:只需包含一个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
    

    这可行,但我对 Image.any_in map/reduce 的性能仍然有些怀疑。我认为该 map/reduce 可能有更好的解决方案,但尚未找到。

    【讨论】:

      【解决方案2】:

      试试mongoid_taggable 宝石。它确实希望您正在寻找。

      【讨论】:

        【解决方案3】:

        mongoid-tags-arent-hard gem 似乎比我见过的其他 gem 更强大(并且维护得更积极)。

        【讨论】:

          【解决方案4】:

          这里是 map/reduce 和实时聚合策略的“可扩展”解决方案mongoid_taggable_with_context

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-02-18
            • 2012-05-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多