删除 has_many :through 通过链接关联
我正在尝试删除通过 Appearance
模型关联的 Actor
和 Movie
之间的关联。我想通过单击演员编辑页面上的链接来完成此操作。我找到了需要使用的方法 (@actor.movies.delete()
),但我不确定将其放在哪里以及如何获取正确的电影
ID。
下面是我对 actor 表单中的电影字段的部分内容:
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove Movie" %>
如果我添加 :allow_destroy => ,上面的代码就可以工作。 true
我的 Actor
模型,但我想要做的是重写它,这样它就不会删除电影,只会删除关联。
I'm trying to remove the association between an Actor
and a Movie
, associated through an Appearance
model. I'd like to do this by clicking a link on the actor's edit page. I have found the method I need to use (@actor.movies.delete()
), but I'm unsure where to put it and how to get the right movie
id.
Below is my partial for the movie fields in the actor form:
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove Movie" %>
The above works if I add :allow_destroy => true
to my Actor
model, but what I want to do is rewrite it so it doesn't delete the movie, only the association.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看这里:
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
Have a look here:
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
当然,我需要做的是删除外观而不是电影对象本身。
What I needed to do was, of course, delete the Appearances rather than the movie objects themselves.