How to toggle a boolean field for multiple records generating just one SQL query

Viewed 51

I'm trying to write a migration to update a boolean field where the field will mean the exact opposite of what it means currently. Therefore I need to toggle every record to update the records where this field is true to false and vice-versa.

Example:

class ChangePostsVisibleToArchived < ActiveRecord::Migration[6.1]
  def change
    rename_column :posts, :visible, :archived

    # Toggle all tracked topics
    # What I DON'T want to do:
    Post.all.each { |post| post.toggle! :archived }
  end
end

The way I described above will generate one SQL command per Post record. Is there a way I can toggle all records within a single SQL command using rails ActiveRecord syntax?

1 Answers

Post.update_all "archived = NOT archived"

Related