views:

33

answers:

1

My association option does not appear to be honored.

class ClassRoom < ActiveRecord::Base
  has_many :class_assignments, :dependent => :destroy
  has_many :people, :through=>:class_assignments

class Person < ActiveRecord::Base
  has_many :class_assignments, :dependent => :destroy
  has_many :class_rooms, :through=>:class_assignments

class ClassAssignment < ActiveRecord::Base
  belongs_to :person
  belongs_to :class_room

That is to say, when either a person, or a class room is deleted, the record in the join table/model should also be deleted.

However, ClassRoom.last.destroy destroys the ClassRoom, but no any of it's associated ClassAssignments.

I know :dependent=>:destroy is ignored when :through is used, but I should be able to use it on the join model right?

A: 

try

  class ClassRoom < ActiveRecord::Base 
  def before_destroy
    self.class_assignments.destroy_all
  end

same for person

elmac