ber*_*kes 6 tagging mongodb mongoid ruby-on-rails-3
使用MongoID在Rails中标记的好方法是什么?
似乎只是在文档中添加哈希或数组非常简单,但我不确定这是否是最好的方法.
也许有些宝石?或者是嵌套文档的简单技巧?
现在,我使用了一种非常简单的方法,效果很好:只需包含一个Array- 字段。
#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
Run Code Online (Sandbox Code Playgroud)
这可行,但我对 Image.any_in map/reduce 的性能仍然有点怀疑。我认为该映射/归约可能有更好的解决方案,但尚未找到。
| 归档时间: |
|
| 查看次数: |
1789 次 |
| 最近记录: |