Let's say we have two models with a many-to-many relation:
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
Now if I create an article with a tag: Tag.create(articles: [ Article.create ])
If I run Tag.first.delete
now, it returns false since there is a foreign key constraint due to the many-to-many relationship. If I run Tag.first.delete!
it deletes the Tag but not the association record in the article_tags
table.
If I use dm-contraints
and set everything to :destroy
it also destroys the Article which is not what I want.
I can do
tag = Tag.first
tag.articles = []
tag.save
tag.destroy
but this seems seems unclean. Is there a better way?