Remove N+1 from grid object models

Reimplementing `character` `summon` and `weapon` was making N+1s which made queries really slow
This commit is contained in:
Justin Edmund 2025-02-07 01:53:21 -08:00
parent 4afccabaaf
commit b86f3a90f6
3 changed files with 5 additions and 16 deletions

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true
class GridCharacter < ApplicationRecord
has_one :object, class_name: 'Character', foreign_key: :id, primary_key: :character_id
belongs_to :character, foreign_key: :character_id, primary_key: :id
belongs_to :awakening, optional: true
belongs_to :party,
@ -78,10 +78,6 @@ class GridCharacter < ApplicationRecord
'aetherial_mastery')
end
def character
Character.find(character_id)
end
def blueprint
GridCharacterBlueprint
end

View file

@ -1,19 +1,16 @@
# frozen_string_literal: true
class GridSummon < ApplicationRecord
belongs_to :summon, foreign_key: :summon_id, primary_key: :id
belongs_to :party,
counter_cache: :summons_count,
inverse_of: :summons
validates_presence_of :party
has_one :object, class_name: 'Summon', foreign_key: :id, primary_key: :summon_id
validate :compatible_with_position, on: :create
validate :no_conflicts, on: :create
def summon
Summon.find(summon_id)
end
def blueprint
GridSummonBlueprint
end

View file

@ -1,13 +1,13 @@
# frozen_string_literal: true
class GridWeapon < ApplicationRecord
belongs_to :weapon, foreign_key: :weapon_id, primary_key: :id
belongs_to :party,
counter_cache: :weapons_count,
inverse_of: :weapons
validates_presence_of :party
has_one :object, class_name: 'Weapon', foreign_key: :id, primary_key: :weapon_id
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
belongs_to :weapon_key3, class_name: 'WeaponKey', foreign_key: :weapon_key3_id, optional: true
@ -33,10 +33,6 @@ class GridWeapon < ApplicationRecord
GridWeaponBlueprint
end
def weapon
Weapon.find(weapon_id)
end
def weapon_keys
[weapon_key1, weapon_key2, weapon_key3].compact
end