# Destroying very large object

**URL:** <https://www.ruby-forum.com/t/destroying-very-large-object/92476>\
**Category:** Rails\
**Created:** [April 19, 2007, 9:20pm UTC](https://www.ruby-forum.com/t/destroying-very-large-object/92476 "2007-04-19T21:20:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 19, 2007, 9:20pm UTC](https://www.ruby-forum.com/t/destroying-very-large-object/92476/1 "2007-04-19T21:20:44Z")

</div>

The situation is:

Workout has\_many WorkoutFiles, :dependant =\> :destroy  
WorkoutFile has\_many WorkoutData, :dependant =\> :destroy

There’s a lot of workout data – several thousand records per file

Workout#destroy takes a very long time (20-30 seconds) because it has  
to destroy so many WorkoutData records from the DB.

I tried the following solution:  
First, I dropped the dependant option on the Workout Class.

Then, destroy the workout, render the new page, and afterwards destroy  
the files (and the dependant data). This however doesn’t work, it  
chugs along destroying the data before it updates the page.

How do I get around this?

Thanx!!!

–Code–

def destroy  
workout = Workout.find(params[:id])  
workoutFiles = workout.workout\_files ##get the workout files  
workout.destroy ##destroy the  
workout  
@user = User.find(current\_user)  
@workouts = @user.workouts  
@workouts\_title = “All Workouts”

#render some updates  
render :update do |page|  
page.replace\_html ‘contentBody’, :partial =\>  
‘workout/workout\_body’  
page.replace\_html ‘info\_bar’, :partial =\> ‘workout/  
workout\_left\_bar’ if params[:left]  
page.replace\_html ‘calendar’, :partial =\> ‘calendar/calendar’  
end

#destroy the files after the pages are updated  
workoutFiles.collect{|w| w.destroy}  
##(20-30 seconds of destroying here)

end

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 19, 2007, 10:43pm UTC](https://www.ruby-forum.com/t/destroying-very-large-object/92476/2 "2007-04-19T22:43:02Z")

</div>

Look at the api for active\_record::base. There are some methods you can  
use  
for mass-deletes. You might use an after\_delete callback and issue your  
own  
delete statement so you can avoid invoking all those callbacks.

in the workout\_file.rb class, try this

has\_many :workout\_data

after\_destroy :delete\_related\_records

protected  
def delete\_related\_records  
ids = self.workout\_data.collect{|w| w.id} (gets all the ids)  
WorkoutData.delete(ids)  
end

That _should_ be faster. You may need to move that up a level to the  
workout and have it collect all the related records… but this is the  
general idea.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 19, 2007, 11:07pm UTC](https://www.ruby-forum.com/t/destroying-very-large-object/92476/3 "2007-04-19T23:07:06Z")

</div>

You could use mass deletes without loading associated objects on  
destroy, with smth like:

Workout.transaction do  
WorkoutData.delete\_all([" workout\_file\_id in (select id from  
workout\_files where workout\_id = ? )“, workout.id])  
WorkoutFile.delete\_all([” workout\_id = ?", workout.id])  
workout.destroy  
end

2007/4/19, Josh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
