diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2019-05-21 23:56:22 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-21 23:56:22 +0900 |
commit | 439145c9a8bc811a98946a620afc69469ea82e84 (patch) | |
tree | 801e0ffb599536daa8bf7f331a2ef9994d54159c /activerecord/lib/active_record/type_caster/connection.rb | |
parent | 0810c076de0d9b7879ee240bb08d31fb7c2f5ac9 (diff) | |
parent | 82a54be0c6a19336008e18f52bc76791adc3bd67 (diff) | |
download | rails-439145c9a8bc811a98946a620afc69469ea82e84.tar.gz rails-439145c9a8bc811a98946a620afc69469ea82e84.tar.bz2 rails-439145c9a8bc811a98946a620afc69469ea82e84.zip |
Merge pull request #36314 from kamipo/fallback_type_casting
Fall back to type casting from the connection adapter
Diffstat (limited to 'activerecord/lib/active_record/type_caster/connection.rb')
-rw-r--r-- | activerecord/lib/active_record/type_caster/connection.rb | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/type_caster/connection.rb b/activerecord/lib/active_record/type_caster/connection.rb index 7cf8181d8e..f43559f4cb 100644 --- a/activerecord/lib/active_record/type_caster/connection.rb +++ b/activerecord/lib/active_record/type_caster/connection.rb @@ -8,21 +8,27 @@ module ActiveRecord @table_name = table_name end - def type_cast_for_database(attribute_name, value) + def type_cast_for_database(attr_name, value) return value if value.is_a?(Arel::Nodes::BindParam) - column = column_for(attribute_name) - connection.type_cast_from_column(column, value) + type = type_for_attribute(attr_name) + type.serialize(value) end - private - attr_reader :table_name - delegate :connection, to: :@klass + def type_for_attribute(attr_name) + schema_cache = connection.schema_cache - def column_for(attribute_name) - if connection.schema_cache.data_source_exists?(table_name) - connection.schema_cache.columns_hash(table_name)[attribute_name.to_s] - end + if schema_cache.data_source_exists?(table_name) + column = schema_cache.columns_hash(table_name)[attr_name.to_s] + type = connection.lookup_cast_type_from_column(column) if column end + + type || Type.default_value + end + + delegate :connection, to: :@klass, private: true + + private + attr_reader :table_name end end end |