Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

quoting: Check if id is a valid method before using it

Need to check if valud also respond_to :id before calling it, otherwise
things could explode.
  • Loading branch information...
commit 4a720a2cdefc1926e0bfa23b8febd71869093c59 1 parent e4cde5d
@arthurnn arthurnn authored
View
4 activerecord/lib/active_record/connection_adapters/abstract/quoting.rb
@@ -43,7 +43,9 @@ def quote(value, column = nil)
# SQLite does not understand dates, so this method will convert a Date
# to a String.
def type_cast(value, column)
- return value.id if value.respond_to?(:quoted_id)
+ if value.respond_to?(:quoted_id) && value.respond_to?(:id)
@astyagun
astyagun added a note

May be a dumb question, but I'm curious, why is existence of :quoted_id important when the value of :id is actually returned?

@arthurnn Collaborator

added on 4893170 , by @tenderlove

@tenderlove Owner

IIRC it's an ActiveModel API requirement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ return value.id
+ end
case value
when String, ActiveSupport::Multibyte::Chars
View
7 activerecord/test/cases/adapters/sqlite3/quoting_test.rb
@@ -95,6 +95,13 @@ def id
end
}.new
assert_equal 10, @conn.type_cast(quoted_id_obj, nil)
+
+ quoted_id_obj = Class.new {
+ def quoted_id
+ "'zomg'"
+ end
+ }
+ assert_raise(TypeError) { @conn.type_cast(quoted_id_obj, nil) }
end
end
end
@astyagun

May be a dumb question, but I'm curious, why is existence of :quoted_id important when the value of :id is actually returned?

Please sign in to comment.
Something went wrong with that request. Please try again.