Commit af45a3ad

mo khan <mo@mokhan.ca>
2013-05-05 04:33:17
add counter cache for photos on creation.
1 parent 051ad5d
Changed files (3)
app/models/photo.rb
@@ -1,6 +1,6 @@
 class Photo < ActiveRecord::Base
   attr_accessible :is_primary, :creation, :image
-  belongs_to :creation
+  belongs_to :creation, :counter_cache => true
   validates :image,  :presence => true
   mount_uploader :image, PhotoUploader
   process_in_background :image
db/migrate/20130505042432_add_photos_count.rb
@@ -0,0 +1,14 @@
+class AddPhotosCount < ActiveRecord::Migration
+  def up
+    add_column :creations, :photos_count, :integer, :default => 0
+
+    Creation.reset_column_information
+    Creation.find_each do |creation|
+      Creation.reset_counters creation.id, :photos
+    end
+  end
+
+  def down
+    remove_column :creations, :photos_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 => 20130505034059) do
+ActiveRecord::Schema.define(:version => 20130505042432) do
 
   create_table "active_admin_comments", :force => true do |t|
     t.integer  "resource_id",   :null => false
@@ -90,6 +90,7 @@ ActiveRecord::Schema.define(:version => 20130505034059) do
     t.string   "image"
     t.boolean  "is_restricted", :default => false, :null => false
     t.string   "watermark"
+    t.integer  "photos_count",  :default => 0
   end
 
   add_index "creations", ["created_at"], :name => "index_creations_on_created_at"