Skip to content

Commit 12dbb80

Browse files
authored
Merge pull request rails#51425 from andrewn617/revert-eliminate-lease-connection-in-type-caster-connection
Revert eliminate lease connection in type caster connection
2 parents 530d771 + 42fede5 commit 12dbb80

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

activerecord/lib/active_record/table_metadata.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def associated_table(table_name)
4444
arel_table = arel_table.alias(table_name) if arel_table.name != table_name
4545
TableMetadata.new(association_klass, arel_table, reflection)
4646
else
47-
type_caster = TypeCaster::Connection.new(klass)
47+
type_caster = TypeCaster::Connection.new(klass, table_name)
4848
arel_table = Arel::Table.new(table_name, type_caster: type_caster)
4949
TableMetadata.new(nil, arel_table, reflection)
5050
end

activerecord/lib/active_record/type_caster/connection.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
module ActiveRecord
44
module TypeCaster
55
class Connection # :nodoc:
6-
def initialize(klass)
6+
def initialize(klass, table_name)
77
@klass = klass
8+
@table_name = table_name
89
end
910

1011
def type_cast_for_database(attr_name, value)
@@ -13,8 +14,20 @@ def type_cast_for_database(attr_name, value)
1314
end
1415

1516
def type_for_attribute(attr_name)
16-
@klass.type_for_attribute(attr_name) || Type.default_value
17+
schema_cache = @klass.schema_cache
18+
19+
if schema_cache.data_source_exists?(table_name)
20+
column = schema_cache.columns_hash(table_name)[attr_name.to_s]
21+
if column
22+
type = @klass.with_connection { |connection| connection.lookup_cast_type_from_column(column) }
23+
end
24+
end
25+
26+
type || Type.default_value
1727
end
28+
29+
private
30+
attr_reader :table_name
1831
end
1932
end
2033
end

0 commit comments

Comments
 (0)