mirror of
https://github.com/Growstuff/growstuff.git
synced 2025-12-31 21:47:48 -05:00
Adding more counter caches
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
##
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
20
db/migrate/20191226024813_crop_harvest_counter_cache.rb
Normal file
20
db/migrate/20191226024813_crop_harvest_counter_cache.rb
Normal 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
|
||||
20
db/migrate/20191226024957_crop_photo_counter_cache.rb
Normal file
20
db/migrate/20191226024957_crop_photo_counter_cache.rb
Normal 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
|
||||
@@ -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
|
||||
23
db/migrate/20191226025225_post_comment_counter_cache.rb
Normal file
23
db/migrate/20191226025225_post_comment_counter_cache.rb
Normal 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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user