Skip to content

fix(chats): prevent blank chat creation from raising ActiveRecord::RecordInvalid - #3183

Open
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/blank-chat-creation
Open

fix(chats): prevent blank chat creation from raising ActiveRecord::RecordInvalid#3183
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/blank-chat-creation

Conversation

@sentry

@sentry sentry Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

This PR addresses the ActiveRecord::RecordInvalid error that occurs when a user attempts to create a chat with blank content.

Previously, ChatsController#create would directly call Chat.start! with the provided content. If the content was blank, Chat.start! would attempt to create a Chat record with a blank title (derived from the blank content) and a UserMessage with blank content. Both of these actions violate Chat and Message model validations, leading to an unhandled ActiveRecord::RecordInvalid error.

This fix implements two layers of protection in ChatsController#create:

  1. Guard Clause: A check is added at the beginning of the create action to immediately redirect the user back to the new chat form with an alert if chat_params[:content] is blank. This prevents invalid data from reaching the model layer.
  2. Error Rescue: A rescue ActiveRecord::RecordInvalid block is added around the Chat.start! call. This acts as a fallback, catching any ActiveRecord::RecordInvalid exceptions that might still occur (e.g., from other unexpected validations) and redirecting the user with a user-friendly error message.

Additionally, the en.yml locale file has been updated to include the translation for the blank content alert message.

Fixes SURE-APP-GC

This PR was automatically generated by Sentry. You can adjust this setting at any time.

@superagent-security

Copy link
Copy Markdown

Manage your Superagent protection

Superagent has paused scans for this repository because this unlinked GitHub App installation has used all three included PR scans.

You have 0 of 3 included PR scans remaining.

Create a free account to continue protection, manage scan settings, review security history, and control which repositories are protected.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 86c69acb86

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".


def create
if chat_params[:content].blank?
return redirect_to new_chat_path, alert: t(".content_blank")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Surface blank-message errors for Turbo frame submissions

When a blank message is submitted through the normal form, the form is inside the sidebar_chat Turbo frame (app/views/chats/new.html.erb), so this redirect only replaces that frame. The notification tray is outside the frame in the application layout, meaning Turbo discards the rendered alert and the flash is consumed by the redirected request; the user simply sees the blank form reset with no explanation. Return a Turbo Stream that updates the notification tray or render an error inside the chat frame.

Useful? React with 👍 / 👎.

Comment on lines +27 to +28
rescue ActiveRecord::RecordInvalid => e
redirect_to new_chat_path, alert: e.record.errors.full_messages.to_sentence

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Limit the validation rescue to chat creation

If start! succeeds but set_last_viewed_chat raises ActiveRecord::RecordInvalid—for example, an existing user has a locale that is no longer in I18n.available_locales—this method has already committed the chat and initial message and triggered its after-commit response job. The broad rescue then redirects to the new-chat form as though creation failed, so retrying creates a duplicate chat and may issue another provider request. Restrict the rescue to start! or wrap creation and the user update in one transaction.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants