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
ruby ruby-on-rails ruby-on-rails-4.2


source to share


No one has answered this question yet

Check out similar questions:

1339
How can I rename a database column in Ruby on Rails migrations?
1023
How do I get the current absolute url in Ruby on Rails?
596
How to use problems in Rails 4
592
How to remove columns using Rails migrations
574
How can I "nicely" format the JSON output in Ruby on Rails?
510
How can I remove a key from a Hash and get the remaining hash in Ruby / Rails?
470
How to redirect to 404 in Rails?
1
rails 5 low-level caching API
0
Creating Attributes with Optional Default Values
0
Issue updating attributes when replacing default accessories



All Articles
Loading...
X
Show
Funny
Dev
Pics