2012-10-12 47 views
1

我有一個龐大而複雜的用戶模型,它看起來是這樣的:無法摧毀父對象

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 

我的問題是,DataMapper的是不是讓我摧毀它。我認爲這是Datamapper不想銷燬一個未銷燬子對象的對象的結果,所以我放入了一個destroy_deep方法,該方法調用destroy_to_followers,links_to_followed,底層註釋和配置文件圖像(這些都被正確銷燬) 。

但是,即使我之後調用user.destroy,用戶也不會被銷燬。沒有任何類型的錯誤消息。是否有某種我缺少的級聯刪除命令?

回答

0

我解決了這個問題。

顯然要調試銷燬,object.errors是沒有用的。而是跟蹤以下例外情況:

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

解決方法是,其中一個子字段未映射回用戶。我有一個類像那個屬於User,但用戶沒有n個喜歡。