Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Attributes in validations should be unique #7354

Closed
wants to merge 1 commit into from

2 participants

@saks

I see duplicated error messages while validations. It happens because of attribute was passed to validate_presence_of two times by different developers. It could be useful if ActiveModel will make attributes unique.

@carlosantoniodasilva

I particularly don't think that Rails should handle developer's mistakes of adding the same field twice to a list of attributes to be validated. If we (as developers) detect the problem (for instance, by seeing duplicated error messages), we can fix it in our code, if we change Rails to ignore the problem, we'll probably never notice the problem exists, and we'll continue doing it over and over.

Thanks for your contribution.

ps: please send new pull requests against the master branch, they'll be backported if needed (unless it's a specific bug that only happens there).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 15, 2012
  1. @saks

    validations should be uniq

    saks authored
This page is out of date. Refresh to see the latest.
View
2  activemodel/lib/active_model/validations/with.rb
@@ -4,7 +4,7 @@ module HelperMethods
private
def _merge_attributes(attr_names)
options = attr_names.extract_options!
- options.merge(:attributes => attr_names.flatten)
+ options.merge(:attributes => attr_names.flatten.uniq)
end
end
View
2  activemodel/test/cases/validations/presence_validation_test.rb
@@ -14,7 +14,7 @@ class PresenceValidationTest < ActiveModel::TestCase
end
def test_validate_presences
- Topic.validates_presence_of(:title, :content)
+ Topic.validates_presence_of(:title, :content, :content)
t = Topic.new
assert t.invalid?
Something went wrong with that request. Please try again.