你如何将它映射到Mongoid或MongoMapper

时间:2012-05-18 00:21:10

标签: ruby-on-rails mongoid mongomapper

嗨,有人可以帮我把它映射到Rails和Mongo,我有两天关于这个主题,但我似乎无法搞清楚,我的意思是在Mongoid或MongoMapper中为这两个集合制作模型。

Users collection

{ "_id" : ObjectId( "4fa883e45ddddbb60c4a6970" ),
   "username" : "joedoe",
   "name" : "joe",
   "created" : "2010-01-15 00:00:00",
   "suscribers" : [ 
      { "suscribe_id" : ObjectId( "4fa884a35ddddbb60c4a6971" ),
        "name" : "Mashable", 
        "tags" : ['Tech', 'News'], 
        "sort" : 0 
      }, 
      { "suscribe_id" : ObjectId( "4fa884a35ddddbb60c4a6972" ),
        "name" : "Joe", 
        "tags" : ['Tech'], 
        "sort" : 1 
      } 
   ] }

Suscribers collection
 { "_id" : ObjectId( "4fa884a35ddddbb60c4a6971" ),
   "name" : "Dan",
   "suscribers_count" : 0,
   "latest" : "2010-01-15 00:00:00" },
 { "_id" : ObjectId( "4fa884a35ddddbb60c4a6972" ),
   "name" : "Fer",
   "suscribers_count" : 0,
   "latest" : "2010-01-15 00:00:00" }

谢谢!

1 个答案:

答案 0 :(得分:0)

我在想这样的事情:

class User
  include Mongoid::Document

  field :username
  field :name
  embeds_many :user_subscribers
end

class UserSubscriber
  include Mongoid::Document

  embedded_in :user
  belongs_to :subscriber
  field :tags, type: Array
  field :sort, type: Integer
end

class Subscriber
  include Mongoid::Document

  field :name
  has_many :user_subscribers
end