Last Updated: February 25, 2016
·
3.593K
· drasch

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.