Skip to content
  • Sean Griffin's avatar
    aee8fb7c
    Don't error when `attributes` is called on a frozen AR model · aee8fb7c
    Sean Griffin authored
    `freeze` will ultimately end up freezing the `AttributeSet`, which in
    turn freezes its `@attributes` hash. However, we actually insert a
    special object to lazily instantiate the values of the hash on demand.
    When it does need to actually instantiate all of them for iteration (the
    only case is `ActiveRecord::Base#attributes`, which calls
    `AttributeSet#to_h`), it will set an instance variable as a performance
    optimization
    
    Since it's just an optimization for subsequent calls, and that method
    being called at all is a very uncommon case, we can just leave the ivar
    alone if we're frozen, as opposed to coming up with some overly
    complicated mechanism for freezing which allows us to continue to modify
    ourselves.
    
    Fixes #17960
    aee8fb7c
    Don't error when `attributes` is called on a frozen AR model
    Sean Griffin authored
    `freeze` will ultimately end up freezing the `AttributeSet`, which in
    turn freezes its `@attributes` hash. However, we actually insert a
    special object to lazily instantiate the values of the hash on demand.
    When it does need to actually instantiate all of them for iteration (the
    only case is `ActiveRecord::Base#attributes`, which calls
    `AttributeSet#to_h`), it will set an instance variable as a performance
    optimization
    
    Since it's just an optimization for subsequent calls, and that method
    being called at all is a very uncommon case, we can just leave the ivar
    alone if we're frozen, as opposed to coming up with some overly
    complicated mechanism for freezing which allows us to continue to modify
    ourselves.
    
    Fixes #17960
Loading