ActiveRecord Reload only Associated Record
Today I learned about some focused reload
methods for ActiveRecord Associations.
Let's say I have an Author
class that can have one magnum opus.
Now suppose I've instantiated an Author
, and his magnum opus gets updated elsewhere. If I try to access that instance's magnum opus again (existing instance, no reloading), I'll get the old cached version:
Now to remedy this, we can reload the author:
But why reload the whole author when you can reload just the association? ActiveRecord has reload_*
methods for each has_one
and belongs_to
association that does just that - it reloads only the associated record.
Docs for has_one and belongs_to.
h/t Matt Polito
Tweet