grimen (owner)

Revisions

gist: 186205 Download_button fork
public
Public Clone URL: git://gist.github.com/186205.git
Embed All Files: show embed
Text only #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
One other cause is your :dependent callbacks.
 
    class Blog < AR::Base
      has_many :posts, :dependent => :destroy
    end
 
This will iterate through every post and call #destroy on it. Use :delete_all if you want to just issue a single delete query. HOWEVER, this won't hit your destroy callbacks on Post.
 
    class Blog < AR::Base
      has_many :posts
      after_destroy :purge_posts
 
    private
      def purge_posts
        # one step better, it only loads a page of posts and hits every post destroy callback
        posts.paginated_each { |p| p.destroy }
 
        # even better, but not very dry
        # cheating a bit w/ a denormalized Comment table,
        # but this is a blog comment and i'm pressed for time :)
        Comment.delete_all :blog_id => id
        Post.delete_all :blog_id => id
    end
 
The solution I've been using for this is WillPaginate.
 
    # instantiate only 30 users at a time
    User.paginated_each do |user|
      user.do_some_stuff!
    end
 
Keep in mind that using Sequel or DM won't make you immune to all these (though DM has a nice identity map that helps in a lot of cases).