Skip to content

Commit

Permalink
Merge pull request #51425 from andrewn617/revert-eliminate-lease-conn…
Browse files Browse the repository at this point in the history
…ection-in-type-caster-connection

Revert eliminate lease connection in type caster connection
  • Loading branch information
byroot committed Mar 26, 2024
2 parents 530d771 + 42fede5 commit 12dbb80
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/table_metadata.rb
Expand Up @@ -44,7 +44,7 @@ def associated_table(table_name)
arel_table = arel_table.alias(table_name) if arel_table.name != table_name
TableMetadata.new(association_klass, arel_table, reflection)
else
type_caster = TypeCaster::Connection.new(klass)
type_caster = TypeCaster::Connection.new(klass, table_name)
arel_table = Arel::Table.new(table_name, type_caster: type_caster)
TableMetadata.new(nil, arel_table, reflection)
end
Expand Down
17 changes: 15 additions & 2 deletions activerecord/lib/active_record/type_caster/connection.rb
Expand Up @@ -3,8 +3,9 @@
module ActiveRecord
module TypeCaster
class Connection # :nodoc:
def initialize(klass)
def initialize(klass, table_name)
@klass = klass
@table_name = table_name
end

def type_cast_for_database(attr_name, value)
Expand All @@ -13,8 +14,20 @@ def type_cast_for_database(attr_name, value)
end

def type_for_attribute(attr_name)
@klass.type_for_attribute(attr_name) || Type.default_value
schema_cache = @klass.schema_cache

if schema_cache.data_source_exists?(table_name)
column = schema_cache.columns_hash(table_name)[attr_name.to_s]
if column
type = @klass.with_connection { |connection| connection.lookup_cast_type_from_column(column) }
end
end

type || Type.default_value
end

private
attr_reader :table_name
end
end
end

0 comments on commit 12dbb80

Please sign in to comment.