ActiveRecord "unexpected" data loss

December, 2020
class Chat < ApplicationRecord
  has_many :messages, dependent: :destroy
end

Let's say we already have some chats with messages in our database.

irb(main):001:0> chat = Chat.first
  Chat Load (0.4ms)  SELECT "chats".* FROM "chats" ORDER BY "chats"."id" ASC LIMIT $1  [["LIMIT", 1]]
=> #<Chat id: 1>

irb(main):002:0> chat.messages
=> #<ActiveRecord::Associations::CollectionProxy [#<Message id: 1>, #<Message id: 2>]>

Creating a new instance of Chat and passing in :id and :messages like so

irb(main):003:0> Chat.new(id: 1, messages: [])
  Message Load (0.9ms)  SELECT "messages".* FROM "messages" WHERE "messages"."chat_id" = $1  [["chat_id", 1]]
   (0.2ms)  BEGIN
  Message Destroy (0.3ms)  DELETE FROM "messages" WHERE "messages"."id" = $1  [["id", 1]]
  Message Destroy (0.2ms)  DELETE FROM "messages" WHERE "messages"."id" = $1  [["id", 2]]
   (1.8ms)  COMMIT
=> #<Chat id: 1>

will actually fire up a DELETE  SQL statement and destroy our messages.