rails:dependent =>:destroy被忽略

时间:2010-08-05 02:22:44

标签: ruby-on-rails associations has-many-through

我的关联选项似乎没有兑现。

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时会被忽略,但我应该可以在连接模型上使用它吗?

1 个答案:

答案 0 :(得分:0)

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

对于人来说相同