clear cache of a Ruby on Rails association
Rails associations cache their values.
user.comments # will cache its value
c = Comment.new(:user => user)
c.save
user.comments.size #(still returns 0)
many have seen the "force reload" trick
user.comments(true) # forces a load from the database
What if I want to make sure the next call to the association loads from the db?
user.comments.reset # this clears the "loaded" flag so next time it will be reloaded.
Written by David Rasch
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#