4
我的車型有:如何刪除嵌套的has_many關聯中關聯的所有對象?
class Campaign < ActiveRecord::Base
has_many :days, dependent: :destroy
end
class Day < ActiveRecord::Base
belongs_to :campaign
has_many :time_slots
before_destroy { time_slots.destroy_all }
end
class TimeSlot < ActiveRecord::Base
belongs_to :day
has_and_belongs_to_many :users
end
我希望能夠刪除廣告,並有與其關聯的所有日子裏,和時隙刪除。我還想要刪除time_slot_users連接表中的記錄。
我試過使用dependent: :destroy
,但似乎沒有級聯?我應該使用before_destroy
回調嗎?
destroy
和destroy_all
有什麼區別?我已閱讀:http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Delete+or+destroy%3F和區別仍然是模糊的。