3
比方說,我們有兩個型號,一個多一對多的關係:如果我Tag.create(articles: [ Article.create ])
DataMapper的許多一對多刪除約束
:
class Tag
include DataMapper::Resource
property :id, Serial
has n, :articles, through: Resource
end
class Article
include DataMapper::Resource
property :id, Serial
has n, :tags, through: Resource
end
現在,如果我創建一個標籤的物品現在運行Tag.first.delete
,由於存在由多對多關係造成的外鍵約束,因此它返回false。如果我運行Tag.first.delete!
,它會刪除標記,但不會刪除article_tags
表中的關聯記錄。
如果我使用dm-contraints
並將所有設置設置爲:destroy
它也銷燬不是我想要的文章。
我可以做
tag = Tag.first
tag.articles = []
tag.save
tag.destroy
但這似乎看起來不乾淨。有沒有更好的辦法?
xato,你有沒有找到更好的方法? – fernandospr 2014-03-26 17:44:37