From 460ff43a4e245476b3c3534c9311529903e59614 Mon Sep 17 00:00:00 2001 From: Brenda Date: Thu, 26 Dec 2019 16:04:28 +1300 Subject: [PATCH] Adding more counter caches --- app/models/comment.rb | 2 +- app/models/harvest.rb | 4 ++-- app/models/like.rb | 2 +- app/models/photo.rb | 2 +- app/models/photo_association.rb | 4 ++-- ...191226024813_crop_harvest_counter_cache.rb | 20 ++++++++++++++++ ...20191226024957_crop_photo_counter_cache.rb | 20 ++++++++++++++++ ...025124_plant_part_harvest_counter_cache.rb | 23 +++++++++++++++++++ ...191226025225_post_comment_counter_cache.rb | 23 +++++++++++++++++++ db/schema.rb | 6 ++++- 10 files changed, 98 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20191226024813_crop_harvest_counter_cache.rb create mode 100644 db/migrate/20191226024957_crop_photo_counter_cache.rb create mode 100644 db/migrate/20191226025124_plant_part_harvest_counter_cache.rb create mode 100644 db/migrate/20191226025225_post_comment_counter_cache.rb diff --git a/app/models/comment.rb b/app/models/comment.rb index 35f0c7009..7b85f4c0d 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -2,7 +2,7 @@ class Comment < ApplicationRecord belongs_to :author, class_name: 'Member', inverse_of: :comments - belongs_to :post + belongs_to :post, counter_cache: true scope :post_order, -> { reorder("created_at ASC") } # for display on post page diff --git a/app/models/harvest.rb b/app/models/harvest.rb index 6777e3c6c..a38241c4e 100644 --- a/app/models/harvest.rb +++ b/app/models/harvest.rb @@ -35,8 +35,8 @@ class Harvest < ApplicationRecord ## ## Relationships - belongs_to :crop - belongs_to :plant_part + belongs_to :crop, counter_cache: true + belongs_to :plant_part, counter_cache: true belongs_to :planting, optional: true, counter_cache: true ## diff --git a/app/models/like.rb b/app/models/like.rb index 5cf7992a0..ed16065de 100644 --- a/app/models/like.rb +++ b/app/models/like.rb @@ -2,7 +2,7 @@ class Like < ApplicationRecord belongs_to :member - belongs_to :likeable, polymorphic: true, counter_cache: true + belongs_to :likeable, polymorphic: true, counter_cache: true, touch: true validates :member, :likeable, presence: true validates :member, uniqueness: { scope: :likeable } end diff --git a/app/models/photo.rb b/app/models/photo.rb index 5f9f9169c..38359732c 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -7,7 +7,7 @@ class Photo < ApplicationRecord PHOTO_CAPABLE = %w(Garden Planting Harvest Seed Post Crop).freeze has_many :photo_associations, foreign_key: :photo_id, dependent: :delete_all, inverse_of: :photo - has_many :crops, through: :photo_associations + has_many :crops, through: :photo_associations, counter_cache: true validates :fullsize_url, url: true validates :thumbnail_url, url: true diff --git a/app/models/photo_association.rb b/app/models/photo_association.rb index 425a598c5..1fe576902 100644 --- a/app/models/photo_association.rb +++ b/app/models/photo_association.rb @@ -2,8 +2,8 @@ class PhotoAssociation < ApplicationRecord belongs_to :photo, inverse_of: :photo_associations - belongs_to :photographable, polymorphic: true - belongs_to :crop, optional: true + belongs_to :photographable, polymorphic: true, touch: true + belongs_to :crop, optional: true, counter_cache: true validate :photo_and_item_have_same_owner diff --git a/db/migrate/20191226024813_crop_harvest_counter_cache.rb b/db/migrate/20191226024813_crop_harvest_counter_cache.rb new file mode 100644 index 000000000..d0b9c2082 --- /dev/null +++ b/db/migrate/20191226024813_crop_harvest_counter_cache.rb @@ -0,0 +1,20 @@ +class CropHarvestCounterCache < ActiveRecord::Migration[5.2] + def change + change_table :crops do |t| + t.integer :harvests_count, default: 0 + end + reversible do |dir| + dir.up { set_counter_value } + end + end + def set_counter_value + execute <<-SQL.squish + UPDATE crops + SET harvests_count = ( + SELECT count(1) + FROM harvests + WHERE harvests.crop_id = crops.id + ) + SQL + end +end diff --git a/db/migrate/20191226024957_crop_photo_counter_cache.rb b/db/migrate/20191226024957_crop_photo_counter_cache.rb new file mode 100644 index 000000000..3bb4ebe68 --- /dev/null +++ b/db/migrate/20191226024957_crop_photo_counter_cache.rb @@ -0,0 +1,20 @@ +class CropPhotoCounterCache < ActiveRecord::Migration[5.2] + def change + change_table :crops do |t| + t.integer :photo_associations_count, default: 0 + end + reversible do |dir| + dir.up { set_counter_value } + end + end + def set_counter_value + execute <<-SQL.squish + UPDATE crops + SET photo_associations_count = ( + SELECT count(1) + FROM photo_associations + WHERE photo_associations.crop_id = crops.id + ) + SQL + end +end diff --git a/db/migrate/20191226025124_plant_part_harvest_counter_cache.rb b/db/migrate/20191226025124_plant_part_harvest_counter_cache.rb new file mode 100644 index 000000000..e16deaced --- /dev/null +++ b/db/migrate/20191226025124_plant_part_harvest_counter_cache.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +class PlantPartHarvestCounterCache < ActiveRecord::Migration[5.2] + def change + change_table :plant_parts do |t| + t.integer :harvests_count, default: 0 + end + reversible do |dir| + dir.up { set_counter_value } + end + end + + def set_counter_value + execute <<-SQL.squish + UPDATE plant_parts + SET harvests_count = ( + SELECT count(1) + FROM harvests + WHERE harvests.plant_part_id = plant_parts.id + ) + SQL + end +end diff --git a/db/migrate/20191226025225_post_comment_counter_cache.rb b/db/migrate/20191226025225_post_comment_counter_cache.rb new file mode 100644 index 000000000..df930fe0d --- /dev/null +++ b/db/migrate/20191226025225_post_comment_counter_cache.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +class PostCommentCounterCache < ActiveRecord::Migration[5.2] + def change + change_table :posts do |t| + t.integer :comments_count, default: 0 + end + reversible do |dir| + dir.up { set_counter_value } + end + end + + def set_counter_value + execute <<-SQL.squish + UPDATE posts + SET comments_count = ( + SELECT count(1) + FROM comments + WHERE comments.post_id = posts.id + ) + SQL + end +end diff --git a/db/schema.rb b/db/schema.rb index 06d7be216..4e6e251cd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_11_19_030244) do +ActiveRecord::Schema.define(version: 2019_12_26_025225) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -196,6 +196,8 @@ ActiveRecord::Schema.define(version: 2019_11_19_030244) do t.integer "median_days_to_first_harvest" t.integer "median_days_to_last_harvest" t.jsonb "openfarm_data" + t.integer "harvests_count", default: 0 + t.integer "photo_associations_count", default: 0 t.index ["name"], name: "index_crops_on_name" t.index ["requester_id"], name: "index_crops_on_requester_id" t.index ["slug"], name: "index_crops_on_slug", unique: true @@ -458,6 +460,7 @@ ActiveRecord::Schema.define(version: 2019_11_19_030244) do t.datetime "created_at" t.datetime "updated_at" t.string "slug" + t.integer "harvests_count", default: 0 end create_table "plantings", id: :serial, force: :cascade do |t| @@ -491,6 +494,7 @@ ActiveRecord::Schema.define(version: 2019_11_19_030244) do t.string "slug" t.integer "forum_id" t.integer "likes_count", default: 0 + t.integer "comments_count", default: 0 t.index ["created_at", "author_id"], name: "index_posts_on_created_at_and_author_id" t.index ["slug"], name: "index_posts_on_slug", unique: true end