before_destroy未从update_attributes触发

Chr*_*ris 7 collections ruby-on-rails callback associations

我有一个有很多课程的学生.在学生#upcate动作和表单中,我接受course_ids列表.当该列表发生变化时,我想调用某个函数.我的代码不会被调用,如果update_attributes方法创建一个course_student,但若update_attributes方法销毁course_student被调用.我可以解决这个问题,还是我必须自己检测这些更改?

# app/models/student.rb
class Student < ActiveRecord::Base
  belongs_to :teacher
  has_many :grades
  has_many :course_students, :dependent => :destroy
  has_many :courses, :through => :course_students
  has_many :course_efforts, :through => :course_efforts

  # Uncommenting this line has no effect:
  #accepts_nested_attributes_for :course_students, :allow_destroy => true

  #attr_accessible :first_name, :last_name, :email, :course_students_attributes

  validates_presence_of :first_name, :last_name
...
end

# app/models/course_student.rb
class CourseStudent < ActiveRecord::Base
  after_create  :reseed_queues
  before_destroy :reseed_queues

  belongs_to :course
  belongs_to :student

  private

  def reseed_queues
    logger.debug "******** attempting to reseed queues"
    self.course.course_efforts.each do |ce|
      ce.reseed
    end
  end

end

# app/controllers/students_controller.rb

  def update
    params[:student][:course_ids] ||= []
    respond_to do |format|
      if @student.update_attributes(params[:student])
        format.html { redirect_to(@student, :notice => 'Student was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @student.errors, :status => :unprocessable_entity }
      end
    end
  end
Run Code Online (Sandbox Code Playgroud)

Chr*_*ris 7

事实证明,这种行为是在has_many方法上记录的.从API文档:

collection = objects 通过删除和添加适当的对象来替换集合内容.如果:through选项为true,则会触发连接模型中的回调,但销毁回调除外,因为删除是直接的.

我不确定"因为删除是直接的"意味着什么,但我们有它.