2012-05-15 12 views
20

मेरे पास एक परियोजना मॉडल से हैस्क_एमनी के माध्यम से एक कार्य मॉडल है और एसोसिएशन के माध्यम से हटाने/डालने से पहले डेटा में हेरफेर करने की आवश्यकता है।एसोसिएशन के माध्यम से has_many में कॉलबैक का उपयोग कैसे करें?

"Automatic deletion of join models is direct, no destroy callbacks are triggered." के बाद से मैं इसके लिए कॉलबैक का उपयोग नहीं कर सकता।

कार्य में कार्य के बाद परियोजना के लिए मूल्य की गणना करने के लिए मुझे सभी प्रोजेक्ट_ड्स की आवश्यकता है। एसोसिएशन के माध्यम से has_many पर नष्ट करने के लिए मैं डिलीट को हटा या हटा सकता हूं? इस समस्या के लिए सबसे अच्छा अभ्यास क्या है?

class Task 
    has_many :project_tasks 
    has_many :projects, :through => :project_tasks 

class ProjectTask 
    belongs_to :project 
    belongs_to :task 

class Project 
    has_many :project_tasks 
    has_many :tasks, :through => :project_tasks 

उत्तर

45

लगता है जैसे मैं associations callbacksbefore_add, after_add, before_remove या after_remove

class Task 
    has_many :project_tasks 
    has_many :projects, :through => :project_tasks, 
         :before_remove => :my_before_remove, 
         :after_remove => :my_after_remove 
    protected 

    def my_before_remove(obj) 
    ... 
    end 

    def my_after_remove(obj) 
    ... 
    end 
end 
1

उपयोग करने के लिए यह है कि मैं क्या मॉडल में

किया है:

class Body < ActiveRecord::Base 
    has_many :hands, dependent: destroy 
    has_many :fingers, through: :hands, after_remove: :touch_self 
end 
में

मेरी लिब फ़ोल्डर:

module ActiveRecord 
    class Base 
    private 
    def touch_self(obj) 
     obj.touch && self.touch 
    end 
    end 
end 
संबंधित मुद्दे