Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Typecast 'id' attribute in read_attribute when using custom pks #5661

Merged
merged 2 commits into from

3 participants

@carlosantoniodasilva

Related to this awesome cleanup :heart:.

When using custom primary keys, the id attribute works as an alias to the pk. But in case of read_attribute, when reading the id it was not being typecasted after the change.

Also added a deprecation message to type_cast_code according to the conversation in the same commit.

Thanks!

@tenderlove tenderlove merged commit 130064d into rails:master
@jonleighton
Collaborator

Doh, I didn't realise I had broken the build! Thanks for fixing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
5 activerecord/lib/active_record/attribute_methods/read.rb
@@ -67,7 +67,10 @@ def read_attribute(attr_name)
@attributes_cache.fetch(attr_name.to_s) { |name|
column = @columns_hash.fetch(name) {
return @attributes.fetch(name) {
- @attributes[self.class.primary_key] if name == 'id'
+ if name == 'id'
+ primary_key = self.class.primary_key
+ @columns_hash[primary_key].type_cast(@attributes[primary_key])
+ end
}
}
View
4 activerecord/lib/active_record/connection_adapters/column.rb
@@ -1,4 +1,5 @@
require 'set'
+require 'active_support/deprecation'
module ActiveRecord
# :stopdoc:
@@ -107,6 +108,9 @@ def type_cast(value)
end
def type_cast_code(var_name)
+ ActiveSupport::Deprecation.warn("Column#type_cast_code is deprecated in favor of" \
+ "using Column#type_cast only, and it is going to be removed in future Rails versions.")
+
klass = self.class.name
case type
View
4 activerecord/test/cases/attribute_methods/read_test.rb
@@ -6,10 +6,6 @@ module ActiveRecord
module AttributeMethods
class ReadTest < ActiveRecord::TestCase
class FakeColumn < Struct.new(:name)
- def type_cast_code(var)
- var
- end
-
def type; :integer; end
end
Something went wrong with that request. Please try again.