我有以下3个型号:
class ExitSurveyResponse < ActiveRecord::Base
belongs_to :survey_response, dependent: :destroy
belongs_to :attendance
end
class SurveyResponse < ActiveRecord::Base
has_many :question_responses, class_name: 'SurveyQuestionResponse', dependent: :destroy
has_one :exit_survey_response, dependent: :destroy
end
class SurveyQuestionResponse < ActiveRecord::Base
attr_accessible :response
validates :response, length: { maximum: 255 }
belongs_to :survey_question
belongs_to :survey_response
before_destroy :destroy_attendance_cache
private
def destroy_attendance_cache
logger.debug('The following is nil because it was already deleted')
logger.debug("exit_survey_response: #{survey_response.exit_survey_response}")
end
end
问题是,当我删除ExitSurveyResponse
时,我需要修改与其关联的attendance
记录。但是当我删除它时,我需要在SurveyQuestionResponse
对象中这样做。问题是,如果我这样做,ExitSurveyResponse
已被删除。有没有办法延迟删除ExitSurveyResponse
s,直到家属被销毁为止?
要明确,我总是需要在attendance
被销毁时更新SurveyQuestionResponse
。但是SurveyQuestionResponse
可以直接销毁(易于处理),销毁SurveyResponse
或销毁ExitSurveyResponse
时。