확인해 보니 이는 의도된 동작입니다. find_by를 사용하더라도 deleted_at 값이 있는 기록은 제외되기 때문입니다:
ChatChannel.find_by(slug: "sec-fhir")
ChatChannel Load (0.4ms) SELECT "chat_channels".* FROM "chat_channels" WHERE "chat_channels"."deleted_at" IS NULL AND "chat_channels"."slug" = 'sec-fhir' LIMIT 1
=> nil
대신 다음처럼 해야 합니다 (with_deleted 추가):
ChatChannel.with_deleted.find_by(slug: "sec-fhir")
ChatChannel Load (0.3ms) SELECT "chat_channels".* FROM "chat_channels" WHERE "chat_channels"."slug" = 'sec-fhir' LIMIT 1
=> #<CategoryChannel:0x00007fc9bfb4abf0
id: 124,
chatable_id: 19,
deleted_at: Wed, 15 Feb 2023 01:19:20.982181000 UTC +00:00,
deleted_by_id: nil,
featured_in_category_id: nil,
delete_after_seconds: nil,
chatable_type: "Category",
created_at: Fri, 13 Jan 2023 01:46:43.730329000 UTC +00:00,
updated_at: Wed, 15 Feb 2023 01:19:56.427647000 UTC +00:00,
name: "test channel",
description: "",
status: "archived",
user_count: 1,
last_message_sent_at: Fri, 13 Jan 2023 01:46:51.130903000 UTC +00:00,
auto_join_users: false,
user_count_stale: false,
slug: "sec-fhir",
type: "CategoryChannel",
allow_channel_wide_mentions: true,
messages_count: 0,
threading_enabled: false>
따라서 오래된 삭제된 채널의 슬러그를 정리하려면 다음과 같이 처리해야 합니다:
channel = ChatChannel.with_deleted.find_by(slug: "sec-fhir")
channel.update!(slug: "#{channel.deleted_at.strftime("%Y%m%d-%H%M")}-#{channel.slug}-deleted")
@mcwumbly 와 @j.jaffeux 님, 이를 수정하기 위해 마이그레이션이 필요할까요?