Rails belongs_to association并销毁没有看到原始记录的回调

时间:2014-01-08 03:37:21

标签: ruby-on-rails ruby-on-rails-3 activerecord ruby-on-rails-3.2 ruby-on-rails-4

我有以下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时。

0 个答案:

没有答案