Commit 5de9ec96

mo khan <mo@mokhan.ca>
2013-07-06 10:41:08
add creations counter_cache
1 parent 64fc4ee
app/models/creation.rb
@@ -4,7 +4,7 @@ class Creation < ActiveRecord::Base
   acts_as_commentable
   validates :name,  :presence => true
   attr_accessible :user_id, :story, :name, :category_ids, :is_restricted, :watermark
-  belongs_to :user
+  belongs_to :user, :counter_cache => true
   has_and_belongs_to_many :categories, :join_table => 'creations_categories', :uniq => true, :autosave => true
   has_many :photos, :dependent => :destroy, :order => :created_at
   has_many :favorites, :dependent => :destroy
db/migrate/20130706103848_add_creations_count_to_users.rb
@@ -0,0 +1,13 @@
+class AddCreationsCountToUsers < ActiveRecord::Migration
+  def up
+    add_column :users, :creations_count, :integer, :default => 0
+    User.reset_column_information
+    User.find_each do |user|
+      User.reset_counters user.id, :creations
+    end
+  end
+
+  def down
+    remove_column :users, :creations_count
+  end
+end
db/schema.rb
@@ -11,7 +11,7 @@
 #
 # It's strongly recommended to check this file into your version control system.
 
-ActiveRecord::Schema.define(:version => 20130702213105) do
+ActiveRecord::Schema.define(:version => 20130706103848) do
 
   create_table "active_admin_comments", :force => true do |t|
     t.integer  "resource_id",   :null => false
@@ -229,6 +229,7 @@ ActiveRecord::Schema.define(:version => 20130702213105) do
     t.string   "authentication_token"
     t.string   "invitation_token"
     t.string   "full_address"
+    t.integer  "creations_count",                       :default => 0
   end
 
   add_index "users", ["authentication_token"], :name => "index_users_on_authentication_token", :unique => true