Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kongregate/detached_counter_cache
counter cache that lives in its own table
https://github.com/kongregate/detached_counter_cache
Last synced: 9 days ago
JSON representation
counter cache that lives in its own table
- Host: GitHub
- URL: https://github.com/kongregate/detached_counter_cache
- Owner: kongregate
- License: mit
- Created: 2016-07-20T22:04:19.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-10-31T19:32:28.000Z (about 2 years ago)
- Last Synced: 2024-12-07T20:56:11.358Z (29 days ago)
- Language: Ruby
- Size: 15.6 KB
- Stars: 0
- Watchers: 9
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# detached_counter_cache
[![Build Status](https://travis-ci.org/kongregate/detached_counter_cache.png?branch=master)](https://travis-ci.org/kongregate/detached_counter_cache)
detached_counter_cache is a tool for keeping track of a counter cache that lives in its own table. It works with Rails 4.1 and 4.2, and only MySQL.
## Usage
For example, with `User` and `Comment` classes you may want to cache `user.comments.size`, but don't want to create `comments_count` on `User`.
```ruby
class User < ActiveRecord::Base
has_many :comments
endclass Comment < ActiveRecord::Base
belongs_to :user, detached_counter_cache: true
end
```You'll need to generate a migration that creates your counter cache table.
```ruby
create_table :users_comments_counts, force: true do |t|
t.integer :user_id, null: false
t.integer :count, default: 0, null: false
end
```## Contributors
* [stopdropandrew](https://github.com/stopdropandrew)
* [drewchandler](https://github.com/drewchandler)
* [duncanbeevers](https://github.com/duncanbeevers)## License
MIT License