2011-03-18 18 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 सहेजा जा रहा है और वह भी डेटा जो मैं स्थायी_ड्रेस में पॉप्युलेट करता हूं।

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')}}}) 

मुझे यकीन नहीं है कि यह कुछ है जो मैं गलत कर रहा हूं या कोई अन्य समस्या है। मैं रेल 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

+0

मुझे उपरोक्त के साथ कुछ समस्याएं आ रही हैं। मैं current_address और permanent_address पाने के लिए एक ही फॉर्म का उपयोग कर रहा हूं, लेकिन केवल वर्तमान पता सहेजा जा रहा है। – rubish

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 
संबंधित मुद्दे