Skip to content

Instantly share code, notes, and snippets.

@tpendragon
Forked from anonymous/gist:5856301
Last active December 18, 2015 22:39

Revisions

  1. terrellt revised this gist Jun 25, 2013. 2 changed files with 9 additions and 13 deletions.
    5 changes: 1 addition & 4 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,7 @@
    class UpdateCache < ActiveRecord::Migration
    def up
    User.all.each do |user|
    object = user
    association = :pets
    counter_name = :pets_count
    User.connection.update("UPDATE #{User.quoted_table_name} SET #{User.connection.quote_column_name(counter_name)} = #{object.pets.count} WHERE #{User.connection.quote_column_name(User.primary_key)} = #{User.quote_value(object.id)}", "#{User.name} UPDATE")
    User.reset_counters(user.id, :pets)
    end
    end

    17 changes: 8 additions & 9 deletions gistfile2.txt
    Original file line number Diff line number Diff line change
    @@ -13,16 +13,15 @@
    ** [out :: ]
    ** [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/reflection.rb:172:in `klass'
    ** [out :: ]
    ** [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/association.rb:117:in `
    klass'
    ** [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/counter_cache.rb:33:in `block in res
    et_counters'
    ** [out :: ]
    ** [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/association.rb:123:in `
    target_scope'
    ** [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/counter_cache.rb:19:in `each'
    ** [out :: ]
    ** [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/association.rb:87:in `s
    coped'
    ** [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/counter_cache.rb:19:in `reset_counte
    rs'
    ** [out :: ]
    ** [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.
    rb:210:in `count'
    ** [out :: ] /var/www/Test/releases/20130625071422/db/migrate/20130625055633_update_cache.rb:4:in `block in up'
    ** [out :: ]
    ** [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/collection_proxy.rb:46:
    ** [out :: ] /var/www/Test/releases/20130625071422/db/migrate/20130625055633_update_cache.rb:3:in `each'
    ** [out :: ]
  2. terrellt revised this gist Jun 25, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@ def up
    User.all.each do |user|
    object = user
    association = :pets
    counter_name = :pets_count
    User.connection.update("UPDATE #{User.quoted_table_name} SET #{User.connection.quote_column_name(counter_name)} = #{object.pets.count} WHERE #{User.connection.quote_column_name(User.primary_key)} = #{User.quote_value(object.id)}", "#{User.name} UPDATE")
    end
    end
  3. terrellt revised this gist Jun 25, 2013. 1 changed file with 27 additions and 18 deletions.
    45 changes: 27 additions & 18 deletions gistfile2.txt
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,28 @@
    [out :: ] == UpdatePetsCache: migrating ================================================
    [out :: ]
    [out :: ] rake aborted!
    [out :: ]
    [out :: ] An error has occurred, this and all later migrations canceled:
    [out :: ]
    [out :: ]
    [out :: ] uninitialized constant User::Pet
    [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/inheritance.rb:111:in `compute_type'
    servers: [""]
    [] executing command
    ** [out :: ] == UpdateCache: migrating ====================================================
    ** [out :: ]
    ** [out :: ] rake aborted!
    ** [out :: ]
    ** [out :: ] An error has occurred, this and all later migrations canceled:
    ** [out :: ]
    ** [out :: ]
    ** [out :: ] uninitialized constant User::Pet
    ** [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/inheritance.rb:111:in `compute_type'

    [out :: ]
    [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/reflection.rb:172:in `klass'
    [out :: ]
    [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/counter_cache.rb:33:in `block in res
    ounters'
    [out :: ]
    [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/counter_cache.rb:19:in `each'
    [out :: ]
    [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/counter_cache.rb:19:in `reset_counte
    ** [out :: ]
    ** [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/reflection.rb:172:in `klass'
    ** [out :: ]
    ** [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/association.rb:117:in `
    klass'
    ** [out :: ]
    ** [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/association.rb:123:in `
    target_scope'
    ** [out :: ]
    ** [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/association.rb:87:in `s
    coped'
    ** [out :: ]
    ** [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.
    rb:210:in `count'
    ** [out :: ]
    ** [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/collection_proxy.rb:46:
  4. terrellt revised this gist Jun 25, 2013. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,9 @@
    class UpdatePetsCache < ActiveRecord::Migration
    class UpdateCache < ActiveRecord::Migration
    def up
    User.all.each do |user|
    User.reset_counters user.id, :pets
    object = user
    association = :pets
    User.connection.update("UPDATE #{User.quoted_table_name} SET #{User.connection.quote_column_name(counter_name)} = #{object.pets.count} WHERE #{User.connection.quote_column_name(User.primary_key)} = #{User.quote_value(object.id)}", "#{User.name} UPDATE")
    end
    end

  5. terrellt revised this gist Jun 25, 2013. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  6. terrellt revised this gist Jun 25, 2013. 2 changed files with 22 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions gistfile3.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    class User < ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :token_authenticatable, :confirmable,
    # :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
    :recoverable, :rememberable, :trackable,
    :confirmable
    validates :email, :presence => true, :uniqueness => true
    validates :username, :presence => true, :uniqueness => true

    # Pets
    has_many :pets, :foreign_key => :owner_id
    # @return [Pet] Active pet for this user.
    def active_pet
    self.pets.length > 0 ? self.pets[0] : nil
    end
    end
    5 changes: 5 additions & 0 deletions gistfile4.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    class Pet < ActiveRecord::Base
    belongs_to :owner, :class_name => "User", :counter_cache => true
    validates :owner, :presence => true

    end
  7. @invalid-email-address Anonymous created this gist Jun 25, 2013.
    10 changes: 10 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    class UpdatePetsCache < ActiveRecord::Migration
    def up
    User.all.each do |user|
    User.reset_counters user.id, :pets
    end
    end

    def down
    end
    end
    19 changes: 19 additions & 0 deletions gistfile2.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    [out :: ] == UpdatePetsCache: migrating ================================================
    [out :: ]
    [out :: ] rake aborted!
    [out :: ]
    [out :: ] An error has occurred, this and all later migrations canceled:
    [out :: ]
    [out :: ]
    [out :: ] uninitialized constant User::Pet
    [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/inheritance.rb:111:in `compute_type'

    [out :: ]
    [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/reflection.rb:172:in `klass'
    [out :: ]
    [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/counter_cache.rb:33:in `block in res
    ounters'
    [out :: ]
    [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/counter_cache.rb:19:in `each'
    [out :: ]
    [out :: ] /var/www/Test/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/counter_cache.rb:19:in `reset_counte