MongoID,将文档嵌入多个文档中

rub*_*ish 5 mongoid ruby-on-rails-3

我有一个模型地址,如下所示

class Address
  include Mongoid::Document

  field :line1
  field :city
  # more fields like this

  embedded_in :user, :inverse_of => :permanent_address
  embedded_in :user, :inverse_of => :current_address
  embedded_in :college, :inverse_of => :address
end
Run Code Online (Sandbox Code Playgroud)

有模型学院和用户嵌入地址

class College
  include Mongoid::Document

  references_many :users
  embeds_one :address

  # some fields and more code
end


class User
  include Mongoid::Document

  referenced_in :college, :inverse_of => :users  

  embeds_one :permanent_address, :class_name => "Address"
  embeds_one :current_address, :class_name => "Address"

  # fields and more code
end
Run Code Online (Sandbox Code Playgroud)

我在上面的设置中遇到了一些问题.我正在使用单个表单来询问当前和永久地址以及更多信息,但只有current_address被保存,而且我在permanent_address中填充的数据也是如此.

Parameters: 
  {"utf8"=>"?",
   "authenticity_token"=>"KdOLvzmKyX341SSTc1SoUG6QIP9NplbAwkQkcx8cgdk=", 
   "user"=> {
     "personal_info_attributes"=>{...},
     "nick_names_attributes"=>{...}, 
     "current_address_attributes"=>{
        "line1"=>"", 
        "area"=>"", 
        "country"=>"USA", 
        "postal_code"=>"sd", 
        "city"=>"", 
        "state"=>"", 
        "landmark"=>"", 
        "id"=>"4d891397932ecf36a4000064"
     }, 
     "permanent_address_attributes"=>{
       "line1"=>"", 
       "area"=>"asd", 
       "country"=>"india", 
       "postal_code"=>"", 
       "city"=>"", 
       "state"=>"", 
       "landmark"=>""
     }, 
     "commit"=>"Submit", "id"=>"4d8903d6932ecf32cf000001"}
MONGODB alma_connect['users'].find({:_id=>BSON::ObjectId('4d8903d6932ecf32cf000001')})
MONGODB alma_connect['users'].update({"_id"=>BSON::ObjectId('4d8903d6932ecf32cf000001')}, 
  {"$set"=>{
    "current_address"=>{
      "line1"=>"", 
      "area"=>"asd", 
      "country"=>"india", 
      "postal_code"=>"", 
      "city"=>"", 
      "state"=>"", 
      "landmark"=>"", 
      "_id"=>BSON::ObjectId('4d8916e9932ecf381f000005')}}})
Run Code Online (Sandbox Code Playgroud)

我不确定这是我在这里做错了还是有其他问题.我使用的是Rails 3.0.4和MongoID 2.0.0.rc.7

更新:

我升级到mongoid 2.0.1并更改了我的用户以在地址中包含选项的反转.

class User
  include Mongoid::Document

  referenced_in :college, :inverse_of => :users  

  embeds_one :permanent_address, :class_name => "Address", :inverse_of => :permanent_address
  embeds_one :current_address, :class_name => "Address", :inverse_of => :current_address

  # fields and more code
end
Run Code Online (Sandbox Code Playgroud)

我知道名称的反转是没有意义的,但这里的要点只是为了使它们不同,或者如果你的嵌入类中的关系有良好的名称(如:current_user,:permanent_user),你应该使用它来反转的.

Jul*_*her 1

在我看来很好。我有类似的设置并且它按预期工作。