2012-10-12 48 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个喜欢。