Code Monkey home page Code Monkey logo

custom_counter_cache's People

Contributors

abookyun avatar atitan avatar cainlevy avatar cedric avatar dependabot[bot] avatar dlackty avatar egogo avatar gdpelican avatar h6ah4i avatar ktheory avatar olleolleolle avatar panasyuk avatar st0012 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

custom_counter_cache's Issues

Uninitialized constant User::Counter

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.

More performant example code suggestion

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

updated_at always changed

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.

[Recommendation] Useful information for migration

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

Drop old Rails support?

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'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.