From 42e3b5e04edda58b0366ae17b4d6b0aa2afff595 Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 26 Aug 2013 12:41:10 +1000 Subject: [PATCH] Added plantings_count (i.e. counter_cache) to Member Also removed the superfluous .plantings_count method on Crop, which was causing some confusion. A thing we learned today: we should use .size to find the size of ActiveRecord collections, not .count, because .count doesn't use the cache (while .size does). --- app/models/planting.rb | 2 +- db/migrate/20130826023159_add_plantings_count_to_member.rb | 5 +++++ db/schema.rb | 3 ++- lib/tasks/growstuff.rake | 7 +++++++ script/deploy-tasks.sh | 3 +++ 5 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20130826023159_add_plantings_count_to_member.rb diff --git a/app/models/planting.rb b/app/models/planting.rb index 751b262e5..61188f184 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -6,7 +6,7 @@ class Planting < ActiveRecord::Base :quantity, :sunniness, :planted_from, :owner_id belongs_to :garden - belongs_to :owner, :class_name => 'Member' + belongs_to :owner, :class_name => 'Member', :counter_cache => true belongs_to :crop, :counter_cache => true has_and_belongs_to_many :photos diff --git a/db/migrate/20130826023159_add_plantings_count_to_member.rb b/db/migrate/20130826023159_add_plantings_count_to_member.rb new file mode 100644 index 000000000..06819f61c --- /dev/null +++ b/db/migrate/20130826023159_add_plantings_count_to_member.rb @@ -0,0 +1,5 @@ +class AddPlantingsCountToMember < ActiveRecord::Migration + def change + add_column :members, :plantings_count, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 3c6a2f151..179f7e82b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130826012139) do +ActiveRecord::Schema.define(:version => 20130826023159) do create_table "account_types", :force => true do |t| t.string "name", :null => false @@ -116,6 +116,7 @@ ActiveRecord::Schema.define(:version => 20130826012139) do t.float "longitude" t.boolean "send_notification_email", :default => true t.text "bio" + t.integer "plantings_count" end add_index "members", ["confirmation_token"], :name => "index_users_on_confirmation_token", :unique => true diff --git a/lib/tasks/growstuff.rake b/lib/tasks/growstuff.rake index c0be3f8c3..90e36df29 100644 --- a/lib/tasks/growstuff.rake +++ b/lib/tasks/growstuff.rake @@ -156,6 +156,13 @@ namespace :growstuff do end end + desc "August 2013: initialize member planting counter" + task :initialize_member_planting_count => :environment do + Member.find_each do |m| + Member.reset_counters m.id, :plantings + end + end + end end diff --git a/script/deploy-tasks.sh b/script/deploy-tasks.sh index 4238ae50c..d94f03ed9 100755 --- a/script/deploy-tasks.sh +++ b/script/deploy-tasks.sh @@ -11,3 +11,6 @@ rake growstuff:oneoff:set_default_crop_creator echo "2013-08-26 - set planting owner" rake growstuff:oneoff:set_planting_owner + +echo "2013-08-26 - initialize member planting count" +rake growstuff:oneoff:initialize_member_planting_count