2017-01-16 41 views

回答

4

当使用update_all协会应装载joins,而不是includes。由于includes在另一个查询相关项目的荷载,因此

Post.joins(:comments).where(comments: {id: comment_ids}).update_all(status: 1) 

应该按预期工作

1

试试这个:

posts = Post.joins(:comments).where(comments: {id: comment_ids}) 
posts.update_all(status: 1) 
1

您还可以使用内部查询,以确定需要被更新的帖子。

post_ids = Comment.where(id: comment_ids).select(:post_id) #creates query to select all posts ids 
Post.where(id: post_ids).update_all(status: 1) #executes update query on all posts