2010-08-05 49 views
2

我的關聯選項似乎並未兌現。rails:dependent =>:銷燬被忽略

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 

也就是說,當一個人或一個教室被刪除時,連接表/模型中的記錄也應該被刪除。

但是,ClassRoom.last.destroy破壞了ClassRoom,但沒有任何關聯ClassAssignments

我知道:dependent=>:destroy在使用:through時被忽略,但我應該可以在連接模型上使用它嗎?

回答

0

嘗試

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

相同的人