[英]Rails dependent destroy error
I have a Rails Movie app. 我有一个Rails电影应用程序。 With, obviously, a movie table. 显然有一张电影桌。 A movie has_many :comments, :dependent => :destroy
and a comment belongs_to :movie
. 电影has_many :comments, :dependent => :destroy
和评论属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. 评论也belongs_to :user
因此当新用户在电影中发表评论时,该评论将显示在其users#show
页面上。
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 我也可以转到localhost:3000/comments/:id
来查看该评论的显示页面
Now my problem is this: 现在我的问题是这样的:
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. 我仍然可以转到localhost:3000/comments/:id
,如果我转到users/:id/reviews
(显示用户评论的位置),则会收到一条错误消息,因为该评论仍在显示并且仍属于电影。 So i get an error of this sort Unable to find Movie with id = 58
所以我得到这样的错误Unable to find Movie with id = 58
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]
Movies_controller.rb
有没有一种方法可以说出删除电影时的destroy action
,还可以删除带有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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.