This project is archived and is in readonly mode.
Customization options and validation consistency for validates_uniqueness_of using db constraints
Reported by blythe | December 25th, 2009 @ 04:39 AM
This patch extends #3486 to be consistent with standard AR validations and support customized error message. #3486 patches ActiveRecord to convert db-generated errors for unique constraint violations into active record error messages.
This patch addresses some of those concerns and extends that
functionality to:
1. Add hooks through validates_uniqueness_of to support customized
error messages
# These two are equivalent functions: lazy option skips standard proactive validation query
validates_uniqueness_of :name, :message => 'already used!", :lazy => true
handle_unique_key_violation :name, :message => 'already used!"
# Extend the functionality to all AR objects
ActiveRecord::Base.handle_unique_key_violations
- Keep save! consistent with standard validation
functionality
The current implementation does not add errors for save! and raises RecordNotUnique instead of RecordInvalid exceptions.
- Add error messages to the AR instance as with standard validations. The following functionality would no longer work since no error messages are populated
begin
record.save!
rescue => e
flash[:error] = record.errors.full_messages.first
redirect_to :blah_url
end
- Raise a RecordInvalid subclassed exception for explicitly declared validations. Convenient for rescues_from functionality that looks for RecordInvalid
The patch always adds error messages for save and save!, but only raises RecordInvalid when validates_uniqueness_of is explicitly declared. Conversion of RecordNotUnique to AR errors is exposed in add_record_not_unique_errors to support for further customization (when explicitly handling RecordNotUnique).
The attached patches are based on current (and include #3486)
Comments and changes to this ticket
-
Rohit Arondekar October 8th, 2010 @ 12:31 PM
- State changed from new to stale
- Importance changed from to Low
Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.
-
Rohit Arondekar October 8th, 2010 @ 12:31 PM
Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 3486 Alternative to validates_uniqueness_of using db constraints Since this looks wrapped up, I pulled some changes from m...