Merge pull request #12 from jedmund/counter-cache

Add counter_cache to GridWeapons in Party
This commit is contained in:
Justin Edmund 2022-03-14 18:22:42 -07:00 committed by GitHub
commit d98772e93c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 9 deletions

View file

@ -25,6 +25,7 @@ class Api::V1::PartiesController < Api::V1::ApiController
conditions[:element] = request.params['element'] unless request.params['element'].blank?
conditions[:raid] = request.params['raid'] unless request.params['raid'].blank?
conditions[:created_at] = start_time..now unless request.params['recency'].blank?
conditions[:weapons_count] = 5..13
@parties = Party.where(conditions).each { |party|
party.favorited = (current_user) ? party.is_favorited(current_user) : false

View file

@ -1,5 +1,6 @@
class GridWeapon < ApplicationRecord
belongs_to :party
belongs_to :party,
counter_cache: :weapons_count
belongs_to :weapon_key1, class_name: 'WeaponKey', foreign_key: :weapon_key1_id, optional: true
belongs_to :weapon_key2, class_name: 'WeaponKey', foreign_key: :weapon_key2_id, optional: true

View file

@ -3,9 +3,20 @@ class Party < ApplicationRecord
belongs_to :user, optional: true
belongs_to :raid, optional: true
has_many :characters, foreign_key: "party_id", class_name: "GridCharacter", dependent: :destroy
has_many :weapons, foreign_key: "party_id", class_name: "GridWeapon", dependent: :destroy
has_many :summons, foreign_key: "party_id", class_name: "GridSummon", dependent: :destroy
has_many :characters,
foreign_key: "party_id",
class_name: "GridCharacter",
dependent: :destroy
has_many :weapons,
foreign_key: "party_id",
class_name: "GridWeapon",
dependent: :destroy
has_many :summons,
foreign_key: "party_id",
class_name: "GridSummon",
dependent: :destroy
has_many :favorites
attr_accessor :favorited

View file

@ -0,0 +1,5 @@
class AddWeaponsCountToParties < ActiveRecord::Migration[6.1]
def change
add_column :parties, :weapons_count, :integer
end
end

View file

@ -0,0 +1,7 @@
class PopulatePartyWeaponsCount < ActiveRecord::Migration[6.1]
def up
Party.find_each do |party|
Party.reset_counters(party.id, :weapons)
end
end
end

View file

@ -10,14 +10,13 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_03_09_013333) do
ActiveRecord::Schema.define(version: 2022_03_15_005952) do
# These are extensions that must be enabled in order to support this database
enable_extension "btree_gin"
enable_extension "pg_trgm"
enable_extension "pgcrypto"
enable_extension "plpgsql"
enable_extension "timescaledb"
create_table "characters", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "name_en"
@ -100,9 +99,6 @@ ActiveRecord::Schema.define(version: 2022_03_09_013333) do
t.integer "element"
t.index ["party_id"], name: "index_grid_weapons_on_party_id"
t.index ["weapon_id"], name: "index_grid_weapons_on_weapon_id"
t.index ["weapon_key1_id"], name: "index_grid_weapons_on_weapon_key1_id"
t.index ["weapon_key2_id"], name: "index_grid_weapons_on_weapon_key2_id"
t.index ["weapon_key3_id"], name: "index_grid_weapons_on_weapon_key3_id"
end
create_table "oauth_access_grants", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
@ -153,6 +149,7 @@ ActiveRecord::Schema.define(version: 2022_03_09_013333) do
t.text "description"
t.uuid "raid_id"
t.integer "element"
t.integer "weapons_count"
t.index ["user_id"], name: "index_parties_on_user_id"
end