【发布时间】:2013-04-28 20:51:59
【问题描述】:
假设我们有两个具有多对多关系的模型:
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.create(articles: [ Article.create ])
如果我现在运行Tag.first.delete,它会返回 false,因为由于多对多关系存在外键约束。如果我运行Tag.first.delete!,它会删除标签,但不会删除article_tags 表中的关联记录。
如果我使用dm-contraints 并将所有内容设置为:destroy,它也会破坏我不想要的文章。
我可以的
tag = Tag.first
tag.articles = []
tag.save
tag.destroy
但这似乎不干净。有没有更好的办法?
【问题讨论】:
-
xato,你找到更好的方法了吗?
标签: ruby datamapper ruby-datamapper