【问题标题】:rails :dependent=>:destroy being ignoredrails :dependent=>:destroy 被忽略
【发布时间】:2010-08-05 02:22:43
【问题描述】:

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

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=&gt;:destroy 在使用:through 时会被忽略,但我应该可以在连接模型上使用它吧?

【问题讨论】:

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


    【解决方案1】:

    试试

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

    人也一样

    【讨论】:

      猜你喜欢
      • 2011-02-17
      • 2011-08-28
      • 1970-01-01
      • 1970-01-01
      • 2010-10-21
      • 2016-10-23
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多