ruby-on-rails-3 – 使用MongoID标记Rails的好方法

前端之家收集整理的这篇文章主要介绍了ruby-on-rails-3 – 使用MongoID标记Rails的好方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用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

这有效,但我仍然对Image.any_in map / reduce的性能有点怀疑.我认为可能有一个更好的解决方案,但尚未找到它.

原文链接:https://www.f2er.com/ruby/264823.html

猜你在找的Ruby相关文章