A better way of controlling cache dependencies than belongs_to :product, touch: true
class Author < ActiveRecord::Base
has_many :artciles
cache_depends_on :articles
end
class Artcile < ActiveRecord::Base
belongs_to :rating
cache_depends_on :rating
end
class Rating < ActiveRecord::Base
has_one :article
end
CacheDependsOn
can deal with any kinds of ActiveRecord associations.
...console/SQL log...
...why...
If an Author has five articles and all of them are updated within a transaction, the Author will be updated only once.
...why it is important... ...how to establish 'ideal relationships' between ActiveRecord models.
Add the following to your Gemfile
:
gem 'cache_depends_on'
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
Bug reports and pull requests are welcome on GitHub at https://github.com/veeqo/cache_depends_on.
Sponsored by Veeqo