Skip to content

Commit 640e398

Browse files
committed
Revert "Eliminate lease_connection call in TypeCaster::Connection"
This reverts commit 6dd1929. This introduced a change in behaviour since type_for_attribute is aware of custom types but lookup_cast_type does not. Additionally, since we no longer to use the table and column info to get attributes, this introduced an issue where attribute types were not be correctly found for some queries, where we were joining a table that has a different name than the name of the reflection for that association.
1 parent a2d2155 commit 640e398

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.lease_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)