2011-03-18 89 views
5

我有一個模型地址的文件像下面MongoID,嵌入在多個文檔

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 

有機型學院和用戶其嵌入地址

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 

我得到的一些問題與上面的設置。我使用單一表單來詢問當前和永久地址以及一些更多信息,但只有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')}}}) 

我不確定這是我做錯了還是還有其他一些問題。我使用Rails 3.0.4和MongoID 2.0.0.rc.7

更新:

我升級到2.0.1 mongoid和改變了我的用戶包括在地址的選擇相反。

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 

我知道名字的倒數就沒有意義了,但這裏的要點就是使他們不同,或者如果您有好的名字在嵌入式階級關係(如:CURRENT_USER,:permanent_user) ,你應該使用它來反轉。

回答

1

對我很好。我有一個類似的設置,並按預期工作。

+0

感謝您的確認 – rubish 2011-03-19 14:04:54

+0

我正面臨着上述問題。我使用單個表單來獲取current_address和permanent_address,但只有當前地址正在保存。 – rubish 2011-03-22 21:42:23

1

從Mongoid docs

當一個孩子嵌入文檔可以屬於不止一種類型的主文件,你可以告訴Mongoid通過將作爲選項對父母的定義,以支持這一點,和多態對孩子的選擇。在子對象上,將存儲指示父類型的附加字段。

class Band 
    include Mongoid::Document 
    embeds_many :photos, as: :photographic 
    has_one :address, as: :addressable 
end 

class Photo 
    include Mongoid::Document 
    embedded_in :photographic, polymorphic: true 
end 

class Address 
    include Mongoid::Document 
    belongs_to :addressable, polymorphic: true 
end