Ruby on rails 3 使用MongoID在Rails中进行标记的良好解决方案

Ruby on rails 3 使用MongoID在Rails中进行标记的良好解决方案,ruby-on-rails-3,mongodb,mongoid,tagging,Ruby On Rails 3,Mongodb,Mongoid,Tagging,使用MongoID在Rails中标记有哪些好的解决方案 似乎向文档中添加散列或数组非常简单,但我不确定这是否是最好的方法 也许是宝石?还是使用嵌套文档的简单技巧?目前,我使用了一种非常简单的方法,效果非常好:只需包含一个数组-字段 #app/models/image.rb class Image include Mongoid::Document include Mongoid::Timestamps field :message, :type => String fie

使用MongoID在Rails中标记有哪些好的解决方案

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


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

目前,我使用了一种非常简单的方法,效果非常好:只需包含一个
数组
-字段

#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
#app/models/image.rb
阶级形象
include Mongoid::Document
包含Mongoid::时间戳
字段:消息,:type=>String
字段:标记,:type=>Array
def self.images_用于标记
Image.any_in(:tags=>[tag])
结束
结束
#routes.rb
匹配“tag/:tag”=>“图像#tag”
#/app/controller/images\u controller.rb
类ImagesController<应用程序控制器
#获取/标记
#GET/tag.xml
def标签
@images=Image.images\u用于参数[:tag]
回应待办事项|格式|
format.html{render“index”}
format.xml{render:xml=>@images}
结束
结束
结束
这是可行的,但我仍然对图像的性能有点怀疑。地图/缩略中的任意_。我认为对于map/reduce可能有更好的解决方案,但还没有找到。

试试gem。它确实需要你去寻找。

这里它是一个用于映射/减少和实时聚合策略的“可伸缩”解决方案

与我所见过的其他gem相比,gem似乎更有能力(而且维护更积极)