小编Chr*_*ris的帖子

before_destroy未从update_attributes触发

我有一个有很多课程的学生.在学生#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 …
Run Code Online (Sandbox Code Playgroud)

collections ruby-on-rails callback associations

7
推荐指数
1
解决办法
2212
查看次数