I have a student who has many courses. In action and # student update form, I accept the course_ids list. When this list changes, I would like to call a specific function. The code that I have is called if update_attributes creates a student course, but it does not call if update_attributes destroys a student course. Can I make it shoot, or do I need to detect the changes myself?
# 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
collections callback ruby-on-rails associations
Chris
source share