Search code examples
ruby-on-railsrubyactiverecordruby-on-rails-5data-migration

how to delete using data_migration?


I wanted to know how to complete this method to delete conversation classes from September 7th, 2021 onwards

class RemovesConversationClass < ActiveRecord::Migration[5.2]

  def up
    ConversationClass.where("created_at > ")destroy_all
  end

  def down
    raise ActiveRecord::IrreversibleMigration
  end

end

Solution

  • You could get time like so Time.new(2021, 9, 7)

    ConversationClass.where("created_at > ?", Time.new(2021, 9, 7)).destroy_all
    

    should work. But you should test it in your development environment first before you use it in production.