0

嗨,有人可以帮我将它映射到 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" }

谢谢!

4

1 回答 1

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
于 2012-05-18T22:24:26.827 回答