2011-03-31 17 views
5

मैं mongoid rails3mongid एम्बेडेड दस्तावेज़ कॉलबैक

class Address 
    include Mongoid::Document 
    embedded_in :person, :inverse_of => :address 
    after_validation :call_after_validation 
    before_validation :call_before_validation 
    before_update :call_before_update 
    after_update :call_after_update 
    after_create :call_after_create 
    before_create :call_before_create 

    field :address1 
    field :address2 

    private 
    def call_after_validation 
    puts "After validation callback fired." 
    end 

    def call_before_validation 
    puts "Before validation callback fired." 
    end 

    def call_before_update 
    puts "Before update callback fired." 
    end 

    def call_after_update 
    puts "After update callback fired." 
    end 

    def call_after_create 
    puts "After create callback fired." 
    end 

    def call_before_create 
    puts "Before create callback fired." 
    end 



end 

class Person 
    include Mongoid::Document 
    embeds_one :address 

    field :name 
end 

के साथ एक निम्नलिखित मॉडल अब मैं एक ही बार में व्यक्ति और पता को बचाने के लिए नेस्टेड फार्म का उपयोग किया है।

लेकिन सब के बाद/पहले/अद्यतन कॉलबैक पते के लिए/before_validation

के बाद के लिए कोई सुझाव के लिए छोड़कर निकाल दिया नहीं कर रहे हैं बनाने के क्यों के बाद/पहले/अद्यतन जब नेस्टेड रूप से बनाए गए कॉलबैक पते के लिए निकाल दिया जा रहा है बनाने के?

धन्यवाद

+0

अद्यतन: मूल दस्तावेज़ पर सच मैं mongoid संस्करण 2.0.0 beta19 – Gagan

उत्तर

4

Mongoid केवल दस्तावेज है कि हठ कार्रवाई पर मार डाला गया था की कॉलबैक सक्रिय करता है।

इसलिए, इस मामले में, केवल सत्यापन कॉलबैक पते के लिए आग लग जाएगी क्योंकि पता व्यक्ति में एम्बेड किया गया है। व्यक्ति के लिए बनाएँ/अपडेट कॉलबैक कहा जाएगा।

+0

उपयोग कर रहा हूँ वहाँ किसी भी तरह से या हैक है जो एम्बेडेड द्वारा डॉक्स पहले_save/after_save कॉलबैक निकाल दिया जाएगा या इसी तरह की कार्रवाई हासिल की जा सकती है। – Gagan

+0

ऐसा मत सोचो, लेकिन आप जो भी चाहते हैं उसे करने के लिए आप व्यक्ति मॉडल में कुछ कोड जोड़ सकते हैं, हाँ? – BenB

25

आप cascade_callbacks उपयोग कर सकते हैं:

embeds_one: बच्चे, cascade_callbacks: सच

+0

धन्यवाद! हालांकि अक्सर यह सेटिंग आवश्यक नहीं है, कुछ मामलों के लिए यह एक बहुत उपयोगी चीज है .. – fifigyuri

+0

धन्यवाद। बहुत उपयोगी जवाब। –

संबंधित मुद्दे