我的关联选项似乎没有兑现。
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 :(得分:0)
试
class ClassRoom < ActiveRecord::Base
def before_destroy
self.class_assignments.destroy_all
end
对于人来说相同