What do has_rich_text's encrypted: true and store_if_blank: false options change about how Action Text stores a lesson's notes?
answer
- ActionText::EncryptedRichText subclass
- encrypts :body, non-deterministic
- needs Active Record encryption keys
- embedded files are not encrypted
- store_if_blank: false skips empty rows
basics
~20 sencrypted: true makes the attribute use ActionText::EncryptedRichText, which encrypts the body column with Active Record encryption (non-deterministic). store_if_blank: false stops a blank value from creating a rich-text row and marks an existing one for destruction.
solid answer
~40 s`has_rich_text :notes, encrypted: true` points the generated `has_one` at `ActionText::EncryptedRichText`, a subclass of `RichText` with `encrypts :body`. Rows stay in `action_text_rich_texts`, but `body` holds ciphertext produced with non-deterministic Active Record encryption, so the app needs `active_record_encryption` keys (generated by `bin/rails db:encryption:init`) and SQL can no longer search or compare the HTML. Embedded images are separate Active Storage blobs and are not encrypted by this option. `store_if_blank: false` changes the writer: a blank value builds no row, and blanking existing notes marks the row for destruction on save, instead of the default of storing an empty body. Both are declared per attribute, and encrypting existing content needs a data migration to re-save old rows.
code
ruby · 8 linesclass Lesson < ApplicationRecord
has_rich_text :notes
has_rich_text :private_notes, encrypted: true
has_rich_text :homework, store_if_blank: false
end
lesson.rich_text_private_notes.class # => ActionText::EncryptedRichText
lesson.update!(homework: "") # no row created, or the existing one destroyedgo deeper
Recall that has_rich_text accepts encrypted: true to encrypt the stored HTML.
Explain EncryptedRichText, the keys it needs, why encrypted bodies cannot be searched, and what store_if_blank: false does on blank input.
Plan adoption: migrating existing rows, embedded files left unencrypted, plaintext copies in caches and logs, and which attributes deserve encryption.
Decide the data-protection approach for user-authored content across database, storage and caches, and where application-level encryption is worth its costs.
## The options on has_rich_text `has_rich_text` takes three options in Rails 8.1: | Option | Default | Effect | |---|---|---| | `encrypted:` | `false` | `true` uses `ActionText::EncryptedRichText`, which encrypts `body` | | `store_if_blank:` | `true` | `false` skips saving blank values and removes an existing row when blanked | | `strict_loading:` | the model's `strict_loading_by_default` | `true` raises when the association is lazily loaded | ## encrypted: true A music teacher's private notes about a student (progress, health, family circumstances) may deserve encryption at rest: ```ruby class Lesson < ApplicationRecord has_rich_text :notes # visible to student and parents has_rich_text :private_notes, encrypted: true end ``` What changes: - The association's class becomes **`ActionText::EncryptedRichText`**, which is just `class EncryptedRichText < RichText; encrypts :body; end`. - Rows still live in **`action_text_rich_texts`**; only the `body` value is stored as Active Record encryption ciphertext. `name`, `record_type` and `record_id` remain readable. - Encryption is **non-deterministic**: the same HTML encrypts differently each time, so `WHERE body = ...` or `LIKE` searches cannot match. - Reading `lesson.private_notes` decrypts transparently; rendering and sanitizing work as usual. What it needs and what it does not cover: 1. **Keys.** Active Record encryption must be configured; `bin/rails db:encryption:init` prints a key set to add to credentials under `active_record_encryption`. Without them, saving raises `ActiveRecord::Encryption::Errors::Configuration` ("Missing Active Record encryption credential"). 2. **Embedded files stay plain.** Images in the content are Active Storage blobs stored by the storage service; the option encrypts only the HTML that references them. 3. **Existing rows.** Switching an attribute to encrypted does not rewrite old bodies. Plan a data migration that re-saves them, and expect reads of old unencrypted rows to need Active Record encryption's support for unencrypted data during the transition. 4. **Logs and caches.** Rendered HTML in fragment caches or logs is plaintext; encryption protects the column, not every copy. ## store_if_blank: false By default the writer always assigns the body, so submitting an empty editor creates or keeps a rich-text row with an empty body. With `store_if_blank: false` the generated writer becomes: - **blank value, no existing notes** - nothing is built, so no row is saved; - **blank value, existing notes** - the body is cleared and the row is **marked for destruction**, so autosave deletes it with the lesson's save; - **present value** - assigned as usual. This keeps the table free of empty rows for optional fields, such as homework that most lessons do not have, and makes `lesson.homework?` reflect whether a row exists. ## strict_loading: Passing `strict_loading: true` marks `rich_text_<name>` strict, so a missed preload raises instead of issuing a query; the default follows the model's `strict_loading_by_default` class attribute (false unless set). ## Choosing per attribute - Encrypt rich text that holds personal data and is never searched in SQL; keep searchable, public content unencrypted. - Use `store_if_blank: false` for optional fields; keep the default where code expects a row to exist after any save. - Remember that options are per attribute, so one model can mix them. ## Migrating an existing attribute to encrypted 1. Configure Active Record encryption keys in every environment first. 2. If old rows must stay readable during the rollout, enable `config.active_record.encryption.support_unencrypted_data` temporarily, so plaintext bodies can still be read. 3. Deploy `encrypted: true`, then re-save existing rows in batches, for example with `find_each` over the rich-text records, so each body is written encrypted. 4. Verify no plaintext rows remain, then turn the unencrypted-data support off again. Key rotation, previous keys and deterministic encryption are part of Active Record encryption itself and apply here unchanged.
- Can you search encrypted rich text with a SQL LIKE query?No. `ActionText::EncryptedRichText` uses non-deterministic Active Record encryption, so the stored body is ciphertext that differs on every save. Search has to happen after decryption in Ruby, or through a separate index built from `to_plain_text` that you accept storing in another form.
- Does encrypted: true protect images embedded in the notes?No. Embedded files are Active Storage blobs stored by the configured storage service, and the rich text body only holds `<action-text-attachment>` references to them. Protecting the files themselves is a matter of the storage service and how their URLs are delivered.
- encrypted: true moves the notes into a separate encrypted table
- Encrypted rich text can still be searched with LIKE because encryption is deterministic
- encrypted: true also encrypts embedded images in the storage service
- store_if_blank: false raises a validation error when notes are blank
- encrypted: true works without any Active Record encryption keys configured