简体   繁体   中英

rails :dependent=>:destroy being ignored

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?

try

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

same for person

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM