ruby on rails 4 - Cascading multiple models -
i'm deleting place , it's cascading rows of placeupload, cascade rows of match , tagcostumer while deleting place. how can that?
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
the solution use destroy , create callback automatically deep cascading.
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 |place| tagscustomer.where(place_upload_id: place.id).destroy_all match.where(place_upload_id: place.id).destroy_all end end end
Comments
Post a Comment