2017-01-16 84 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