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

Prevent `RangeError` for `FinderMethods#exists?` #25271

Merged
merged 2 commits into from Jun 15, 2016

Conversation

Projects
None yet
5 participants
@kamipo
Member

kamipo commented Jun 4, 2016

FinderMethods#exists? should return a boolean rather than raising an exception.

@rails-bot

This comment has been minimized.

rails-bot commented Jun 4, 2016

r? @matthewd

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

Avoid type casting in uniqueness validator
Type casting in uniqueness validator is for a string value truncation.
It was removed at #23523.

@kamipo kamipo force-pushed the kamipo:prevent_range_error_for_exists branch Jun 4, 2016

@kamipo kamipo force-pushed the kamipo:prevent_range_error_for_exists branch Jun 4, 2016

@kamipo

This comment has been minimized.

Member

kamipo commented Jun 12, 2016

Related issues: #18134, #19688, #19717, #24238.

@sgrif

This comment has been minimized.

Member

sgrif commented Jun 13, 2016

Why did the uniqueness validator change from this commit? It seems unrelated to "Prevent RangeError for FinderMethods#exists?"

@kamipo

This comment has been minimized.

Member

kamipo commented Jun 14, 2016

#19688 (comment)

Rather than specifically handling the uniqueness validation, I wonder if it would make more sense to change exists? to always return false if it catches a RangeError.

UniquenessValidator#build_relation catches a RangeError because it includes type casting due to a string value truncation. But a string value truncation was removed at #23523 then type casting in build_relation is no longer necessary. aa06231 removes type casting in build_relation then a RangeError moves to relation.exists?.

https://github.com/kamipo/rails/blob/aa06231/activerecord/lib/active_record/validations/uniqueness.rb#L29-L35

        if relation.exists?
          error_options = options.except(:case_sensitive, :scope, :conditions)
          error_options[:value] = value

          record.errors.add(attribute, :taken, error_options)
        end
      rescue RangeError

"Prevent RangeError for FinderMethods#exists?" will remove the catching a RangeError.

@sgrif

This comment has been minimized.

Member

sgrif commented Jun 15, 2016

Thanks for clarifying. Can you put that in the commit message?

Prevent `RangeError` for `FinderMethods#exists?`
`FinderMethods#exists?` should return a boolean rather than raising an
exception.

`UniquenessValidator#build_relation` catches a `RangeError` because it
includes type casting due to a string value truncation. But a string
value truncation was removed at #23523 then type casting in
`build_relation` is no longer necessary. aa06231 removes type casting in
`build_relation` then a `RangeError` moves to `relation.exists?`.

This change will remove the catching a `RangeError`.

@kamipo kamipo force-pushed the kamipo:prevent_range_error_for_exists branch to 1cf467b Jun 15, 2016

@kamipo

This comment has been minimized.

Member

kamipo commented Jun 15, 2016

Updated the commit message!

@sgrif sgrif merged commit 7980b31 into rails:master Jun 15, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

@kamipo kamipo deleted the kamipo:prevent_range_error_for_exists branch Jun 15, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment