cedric / custom_counter_cache Goto Github PK
View Code? Open in Web Editor NEWCustom counter_cache functionality that supports conditions and multiple models.
Home Page: https://github.com/cedric/custom_counter_cache
License: MIT License
Custom counter_cache functionality that supports conditions and multiple models.
Home Page: https://github.com/cedric/custom_counter_cache
License: MIT License
With regard to the example block code:
define_counter_cache :articles_count do |user|
user.articles.where(state: 'published').count
end
Since this count is quite an expensive operation to be going on in every save / destroy, wouldn't it make more sense to also include an example using increment / decrement / update counter?
Thanks in advance
Currently, this gem uses update_attribute
method internally which always alters the updated_at
field even if the specified counter number is the same as the current cached counter number.
At least, this behavior is not expected in our application, but however, I have no idea whether the current behavior should be changed or not. What do you think about it?
Thanks.
I see that the following test failure with Rail 4.0. Do we still need to support old versions? At present (Jul 2019), Rails 4.1 and 4.0 are not maintained already.
https://travis-ci.com/h6ah4i/custom_counter_cache/builds/119572635
TypeError: Cannot visit Integer
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/arel-4.0.2/lib/arel/visitors/visitor.rb:28:in `rescue in visit'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/arel-4.0.2/lib/arel/visitors/visitor.rb:21:in `visit'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/arel-4.0.2/lib/arel/visitors/to_sql.rb:336:in `visit_Arel_Nodes_Limit'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/arel-4.0.2/lib/arel/visitors/visitor.rb:22:in `visit'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/arel-4.0.2/lib/arel/visitors/to_sql.rb:181:in `visit_Arel_Nodes_SelectStatement'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/arel-4.0.2/lib/arel/visitors/sqlite.rb:12:in `visit_Arel_Nodes_SelectStatement'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/arel-4.0.2/lib/arel/visitors/visitor.rb:22:in `visit'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/arel-4.0.2/lib/arel/visitors/visitor.rb:5:in `accept'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/connection_adapters/abstract/database_statements.rb:13:in `to_sql'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/connection_adapters/abstract/database_statements.rb:25:in `select_all'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/connection_adapters/abstract/query_cache.rb:64:in `select_all'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/querying.rb:36:in `find_by_sql'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/relation.rb:587:in `exec_queries'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/association_relation.rb:19:in `exec_queries'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/relation.rb:471:in `load'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/relation.rb:220:in `to_a'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/relation/finder_methods.rb:316:in `find_take'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/relation/finder_methods.rb:66:in `take'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/associations/singular_association.rb:42:in `find_target'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/associations/association.rb:147:in `load_target'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/associations/association.rb:54:in `reload'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/associations/singular_association.rb:9:in `reader'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/associations/builder/association.rb:70:in `user'
/Users/hasegawa/git/custom_counter_cache/lib/custom_counter_cache/model.rb:71:in `block in update_counter_cache'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-4.0.13/lib/active_support/callbacks.rb:375:in `_run__1008848322819682377__create__callbacks'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-4.0.13/lib/active_support/callbacks.rb:80:in `run_callbacks'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/callbacks.rb:306:in `_create_record'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/timestamp.rb:57:in `_create_record'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/persistence.rb:481:in `create_or_update'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/callbacks.rb:302:in `block in create_or_update'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-4.0.13/lib/active_support/callbacks.rb:393:in `_run__1008848322819682377__save__callbacks'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-4.0.13/lib/active_support/callbacks.rb:80:in `run_callbacks'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/callbacks.rb:302:in `create_or_update'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/persistence.rb:103:in `save'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/validations.rb:51:in `save'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/attribute_methods/dirty.rb:32:in `save'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/transactions.rb:270:in `block (2 levels) in save'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/transactions.rb:330:in `block in with_transaction_returning_status'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/transactions.rb:209:in `transaction'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/transactions.rb:327:in `with_transaction_returning_status'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/transactions.rb:270:in `block in save'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/transactions.rb:285:in `rollback_active_record_state!'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/transactions.rb:269:in `save'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/associations/has_many_association.rb:40:in `insert_record'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/associations/collection_association.rb:469:in `block (2 levels) in _create_record'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/associations/collection_association.rb:373:in `add_to_target'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/associations/collection_association.rb:467:in `block in _create_record'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/associations/collection_association.rb:158:in `block in transaction'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/connection_adapters/abstract/database_statements.rb:203:in `block in transaction'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `within_new_transaction'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/connection_adapters/abstract/database_statements.rb:203:in `transaction'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/transactions.rb:209:in `transaction'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/associations/collection_association.rb:157:in `transaction'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/associations/collection_association.rb:466:in `_create_record'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/associations/collection_association.rb:127:in `create'
/usr/local/Cellar/rbenv/1.1.2/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-4.0.13/lib/active_record/associations/collection_proxy.rb:260:in `create'
I'm seeing this error when deploying to Heroku production but not in development. I'm having a hard time debugging it. Just thought I'd let you know in case you've seen this before.
I've added include CustomCounterCache::Model
to the top of the User model. Then I've added
define_counter_cache :videos_published_count do |user|
user.videos_published.count
end
The migration:
class AddVideosPublishedCountToUsers < ActiveRecord::Migration[5.1]
def change
add_column :users, :videos_published_count, :integer, :default => 0, :null => false
User.reset_column_information
User.find_each do |u|
User.update_counters(u.id, videos_published_count: u.videos_published.length)
end
end
end
The only reference to Counter
I can find in the codebase is in the test directory so it's not making sense.
It would be quite useful to add in the readme to add something like this for the migration:
def up
add_column :users, :articles_count, :integer, default: 0, null: false
User.reset_column_information
User.select(:id).find_each(batch_size: 100) { |u| u.update_articles_count }
end
I added a file, and it got merged, and still GitHub's UI does not render the license thing in the masthead of the page.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.