From 30cd66e1521bc760df45908adb2f7d3ba6683900 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 14 Jan 2018 13:06:18 -0500 Subject: Cached a Pokémon's latest revision MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This removes the need to use a GROUP or DISTINCT in eager-loading Pokémon on the front page. It duplicates data, but provisions are in place to keep the cache up-to-date. When a Pokémon record is updated, it is required that its cache points to the current revision. When a revision is created, it updates the Pokémon's cache to point to it, because a new revision will always be the most recent one, and it is impossible to reorder revisions. This does not affect the number of queries used on the front page (see #1). refs #4 --- .../20180114170238_cache_current_pokemon_revision.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 db/migrate/20180114170238_cache_current_pokemon_revision.rb (limited to 'db') diff --git a/db/migrate/20180114170238_cache_current_pokemon_revision.rb b/db/migrate/20180114170238_cache_current_pokemon_revision.rb new file mode 100644 index 0000000..a0e1747 --- /dev/null +++ b/db/migrate/20180114170238_cache_current_pokemon_revision.rb @@ -0,0 +1,19 @@ +class CacheCurrentPokemonRevision < ActiveRecord::Migration[5.1] + def up + change_table :pokeviewer_pokemon do |t| + t.references :current, null: true + end + + add_foreign_key :pokeviewer_pokemon, :pokeviewer_revisions, + column: :current_id + + Pokeviewer::Pokemon.all.each do |p| + p.current_id = p.revisions.last.id + p.save! + end + end + + def down + remove_column :pokeviewer_pokemon, :current_id + end +end -- cgit 1.4.1