Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check :scope input in Uniqueness validator #30214

Merged
merged 1 commit into from Aug 14, 2017
Merged

Check :scope input in Uniqueness validator #30214

merged 1 commit into from Aug 14, 2017

Conversation

@kirs
Copy link
Member

@kirs kirs commented Aug 12, 2017

Fixes #30156

This patch adds validation for :scope option of the Uniqueness validator.
Without this patch, calling something like validates_uniqueness_of :code, :scope => [:archived => false] would cause NoMethodError with a trace into AR internals.

@rails-bot
Copy link

@rails-bot rails-bot commented Aug 12, 2017

r? @schneems

(@rails-bot has picked a reviewer for you, use r? to override)

@@ -8,6 +8,10 @@ def initialize(options)
raise ArgumentError, "#{options[:conditions]} was passed as :conditions but is not callable. " \
"Pass a callable instead: `conditions: -> { where(approved: true) }`"
end
unless Array(options[:scope]).all? { |scope| scope.respond_to?(:to_sym) }

This comment has been minimized.

@kirs

kirs Aug 12, 2017
Author Member

This check is called on boot time when the code is loaded.

activerecord/lib/active_record/validations/uniqueness.rb Outdated
@@ -8,6 +8,10 @@ def initialize(options)
raise ArgumentError, "#{options[:conditions]} was passed as :conditions but is not callable. " \
"Pass a callable instead: `conditions: -> { where(approved: true) }`"
end
unless Array(options[:scope]).all? { |scope| scope.respond_to?(:to_sym) }
raise ArgumentError, "scope option of the uniqueness validator was passed in unknown format. " \
"Pass a symbol or an array of symbols instead: `scope: :user_id`"

This comment has been minimized.

@rafaelfranca

rafaelfranca Aug 12, 2017
Member

I think we should tell which scope option is incorrect like we did with :conditions

@kirs
Copy link
Member Author

@kirs kirs commented Aug 13, 2017

Thanks, updated the error message.

@rafaelfranca rafaelfranca merged commit 509dfdc into rails:master Aug 14, 2017
1 of 2 checks passed
1 of 2 checks passed
continuous-integration/travis-ci/pr The Travis CI build failed
Details
codeclimate All good!
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants