我想更新的所有帖子,其中評論的ID缺少FROM子句,Update_all職位,其中comment_ids
Post.includes(:comments).where(comments: {id: comment_ids}).update_all(status: 1)
ERROR: missing FROM-clause entry for table "comments"
我想更新的所有帖子,其中評論的ID缺少FROM子句,Update_all職位,其中comment_ids
Post.includes(:comments).where(comments: {id: comment_ids}).update_all(status: 1)
ERROR: missing FROM-clause entry for table "comments"
當使用update_all
協會應裝載joins
,而不是includes
。由於includes
在另一個查詢相關項目的荷載,因此
Post.joins(:comments).where(comments: {id: comment_ids}).update_all(status: 1)
應該按預期工作
試試這個:
posts = Post.joins(:comments).where(comments: {id: comment_ids})
posts.update_all(status: 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
您是否正在更新文章或留言? ;) –