diff --git a/app/models/card.rb b/app/models/card.rb index e50ddc9..01fb46f 100644 --- a/app/models/card.rb +++ b/app/models/card.rb @@ -12,7 +12,7 @@ class Card < ActiveRecord::Base belongs_to :awaken_type belongs_to :dokkan_card, class_name: 'Card', foreign_key: :dokkan_id - has_and_belongs_to_many :links + has_and_belongs_to_many :links, counter_cache: true has_many :evidences diff --git a/db/migrate/20151103193404_add_links_counter_cache_to_cards.rb b/db/migrate/20151103193404_add_links_counter_cache_to_cards.rb new file mode 100644 index 0000000..db716bb --- /dev/null +++ b/db/migrate/20151103193404_add_links_counter_cache_to_cards.rb @@ -0,0 +1,14 @@ +class AddLinksCounterCacheToCards < ActiveRecord::Migration + def up + add_column :cards, :links_count, :integer, default: 0, null: false + + Card.reset_column_information + Card.find_each do |card| + card.update_attribute(:links_count, card.links.count) + end + end + + def down + remove_column :cards, :links_count + end +end diff --git a/db/schema.rb b/db/schema.rb index 5909fed..557a322 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20151020212132) do +ActiveRecord::Schema.define(version: 20151103193404) do create_table "awaken_types", force: :cascade do |t| t.string "name" @@ -36,6 +36,7 @@ ActiveRecord::Schema.define(version: 20151020212132) do t.integer "hp_stat_id" t.integer "atk_stat_id" t.integer "def_stat_id" + t.integer "links_count", default: 0, null: false end add_index "cards", ["awaken_type_id"], name: "index_cards_on_awaken_type_id"