我正在删除一个Place,它正在级联PlaceUpload的行,但在删除Place时,我也希望级联Match和TagCostumer的行。我怎么能这么做?
class Place < ActiveRecord::Base
has_many :place_uploads
end
class PlaceUpload < ActiveRecord::Base
belongs_to :place
has_many :matches
has_many :tags_customers
end
class TagsCustomer < ActiveRecord::Base
belongs_to :place_upload
belongs_to :tag
end
class Match < ActiveRecord::Base
belongs_to :place_upload
belongs_to :customer
end发布于 2016-02-06 21:21:47
解决方案是使用破坏并创建一个回调来自动执行深度级联。
class Place < ActiveRecord::Base
before_destroy :delete_children_objects
has_many :place_uploads, :dependent => :destroy
protected
def delete_children_objects
@places = PlaceUpload.where(place_id: id)
@places.each do |place|
TagsCustomer.where(place_upload_id: place.id).destroy_all
Match.where(place_upload_id: place.id).destroy_all
end
end
endhttps://stackoverflow.com/questions/35167011
复制相似问题