1

I have a large and complicated User model that looks something like this:

class User

  class Link
    include DataMapper::Resource
    property :id, Serial, :key => false

    belongs_to :follower, :model => 'User', :key => true
    belongs_to :followed, :model => 'User', :key => true
  end

  include DataMapper::Resource

  property :id, Serial
  property :username, String, :required => true

  has n, :links_to_followers, :model => 'User::Link', :child_key => [:followed_id]
  has n, :links_to_followed, :model => 'User::Link', :child_key => [:follower_id]
  has n, :comments
  has 1, :profile_image
end

My problem is that Datamapper is not letting me Destroy it. I thought this was a result of Datamapper not wanting to destroy an object with un-destroyed child objects so I put in a method destroy_deep that calls destroy on the links_to_followers, links_to_followed, underlying comments, and the profile image (these are all destroyed correctly).

However, even if I call user.destroy after that, the user is not destroyed. There are no error messages of any kind. Is there some kind of cascading delete command that I am missing?

AlexQueue
  • 6,353
  • 5
  • 35
  • 44

1 Answers1

0

I resolved this.

Apparently to debug destroy, object.errors isn't useful. Instead track exceptions like:

begin
  u.destroy
rescue Exception => e
  p e
end

The solution was that one of the children fields didn't map back to User. I had a class Like that belonged to User, but User didn't have n Likes.

AlexQueue
  • 6,353
  • 5
  • 35
  • 44