Adding more counter caches

This commit is contained in:
Brenda
2019-12-26 16:04:28 +13:00
parent 8bacfa74ad
commit 17fa995a17
10 changed files with 98 additions and 8 deletions

View File

@@ -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

View File

@@ -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
##

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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