Skip to content

Add add_error_to option - #65

Closed
flyinggrizzly wants to merge 1 commit into
Shopify:mainfrom
flyinggrizzly:set-error-on-alt-attr
Closed

Add add_error_to option#65
flyinggrizzly wants to merge 1 commit into
Shopify:mainfrom
flyinggrizzly:set-error-on-alt-attr

Conversation

@flyinggrizzly

@flyinggrizzly flyinggrizzly commented Mar 20, 2026

Copy link
Copy Markdown

In cases where case/accent insensitive collations are not available, it
is useful to add a <attr>_normalized proxy column to drive unique
indexes.

In these cases, should the uniqueness constraint be violated, we want to
show the user errors referencing their actual input, without the
_normalized field name, nor the normalized value.

Usage

class MyModel < ApplicationRecord
  rescue_from_duplicate :name_normalized, add_error_to: :name
end

elmo = MyModel.create!(name: "Elmo")
elmo.name
#=> "Elmo"
elmo.name_normalized
#=> "elmo"

eelmo = MyModel.build(name: "Élmo")
eelmo.valid?
#=> false
eelmo.errors[:name]
#=> "is not unique"

In cases where case/accent insensitive collations are not available, it
is useful to add a `<attr>_normalized` proxy column to drive unique
indexes.

In these cases, should the uniqueness constraint be violated, we want to
show the user errors referencing their actual input, without the
`_normalized` field name, nor the normalized value.
@flyinggrizzly

Copy link
Copy Markdown
Author

Alternative pattern

Could also be add_error_to <attr>, via: <attr>_normalized, which would put all behavior on <attr>_normalized, but redirect all errors to <attr>.

Means we don't have doubled validations and error rescuing.

@flyinggrizzly

Copy link
Copy Markdown
Author

Handling with a simple override of #create_or_update instead.

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.

1 participant