How to override Rails read method cache access attributes?

Many gems override Rails attribute attributes using the following code:

define_method "#{dynamic_name}" do
  value = read_attribute(dynamic_name)
  # Do stuff
end

      

Or that:

belongs_to :user
def user
  # Do stuff
end

      

Rails 4.2 introduced a cache for atttribute methods ( AttributeMethodCache

, ReaderMethodCache

, https://github.com/rails/rails/pull/15429 )

This results in incorrect values ​​when accessing overriden attributes using the above methods. It looks like the return values ​​from the original read methods are cached and returned even after the method is overridden.

What is the recommended procedure for overriding attribute assemblers in Rails 4.2?

+3


source to share





All Articles