DataMapper has n through Resource DELETE (Remove from association) not working

ぐ巨炮叔叔 提交于 2019-12-04 14:19:48

The delete() method, and other methods from Array only work on the in-memory copy of the Collections. They don't actually modify anything until you persist the objects.

Also, all CRUD actions performed on a collection primarily affect the target. A few, like create() or destroy(), will add/remove the intermediary resources in many to many collections, but it's only a side effect of creating or removing the target.

In your case, if you wanted to remove just the first Post, you could do this:

User.first.posts.first(1).destroy

The User.first.posts.first(1) part returns a collection scoped to only the first post. Calling destroy on the collection removes everything in the collection (which is just the first record) and includes the intermediaries.

I managed to do it by doing:

#to add
user_posts = User.first.posts
user_posts << Bolt.first
user_posts.save 

#to remove
user_posts.delete(Bolt.first)
user_posts.save

I think the only way to do it is by working with the instance actions, do your changes on that instance and after you finished, just save it.

It's kind of different from AR, but it should be fine though.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!