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和区别仍然是模糊的。