Rails的依赖摧毁错误 [英] Rails dependent destroy error

查看:206
本文介绍了Rails的依赖摧毁错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails应用程序电影。有了,很明显,电影表。电影的has_many:评论:依赖=> :摧毁和注释 belongs_to的:电影
注释也 belongs_to的:用户所以,当一部电影一个新的评论,该评论会显示在他们的用户#显示页。

I have a Rails Movie app. With, obviously, a movie table. A movie has_many :comments, :dependent => :destroy and a comment belongs_to :movie. A comment also belongs_to :user so when a new User comments on a movie, that comment will display on their users#show page.

如果一个的用户评论的上的电影的,注释将显示其网页上。我也可以去本地主机:3000 /评论/:ID 看到评论的节目页

If a User Comments on a Movie, the comment will display on their page. I can also go to localhost:3000/comments/:id to see that comment's show page

现在我的问题是这样的:

Now my problem is this:

如果我再消灭或删除影片与评论,注释犯规居然被删除。我还可以去本地主机:3000 /评论/:ID ,如果我去了用户/:ID /条(其中显示了用户的评论)我得到一个错误,因为仍然被显示的意见,并仍然属于电影。
所以我得到这样的错误无法找到ID电影= 58

If I then destroy or delete that movie with that comment, the comment doesnt actually get deleted. I can still go to localhost:3000/comments/:id, and if i go to the users/:id/reviews (where the user's comments are displayed) I get an error because that comment is still being displayed and still belongs to a movie. So i get an error of this sort Unable to find Movie with id = 58

有没有一个办法 Movies_controller.rb 破坏行动说当电影被删除,同时删除与 movie_id =&GT所有评论; PARAMS [:ID]

Is there a way in the Movies_controller.rb destroy action to say when the movie is deleted, also delete all comments with movie_id => params[:id]

推荐答案

有另一种方法来删除一个电影评论:

There is another way to delete comments of a movie:

def destroy
 @movie = Movie.find(params[:id])
 @movie.comments.delete_all
 @movie.destroy
end

这篇关于Rails的依赖摧毁错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆