Add counter cache for links
This commit is contained in:
parent
1302454e5e
commit
ab8b944682
3 changed files with 17 additions and 2 deletions
|
@ -12,7 +12,7 @@ class Card < ActiveRecord::Base
|
||||||
belongs_to :awaken_type
|
belongs_to :awaken_type
|
||||||
belongs_to :dokkan_card, class_name: 'Card', foreign_key: :dokkan_id
|
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
|
has_many :evidences
|
||||||
|
|
||||||
|
|
|
@ -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
|
|
@ -11,7 +11,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# 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|
|
create_table "awaken_types", force: :cascade do |t|
|
||||||
t.string "name"
|
t.string "name"
|
||||||
|
@ -36,6 +36,7 @@ ActiveRecord::Schema.define(version: 20151020212132) do
|
||||||
t.integer "hp_stat_id"
|
t.integer "hp_stat_id"
|
||||||
t.integer "atk_stat_id"
|
t.integer "atk_stat_id"
|
||||||
t.integer "def_stat_id"
|
t.integer "def_stat_id"
|
||||||
|
t.integer "links_count", default: 0, null: false
|
||||||
end
|
end
|
||||||
|
|
||||||
add_index "cards", ["awaken_type_id"], name: "index_cards_on_awaken_type_id"
|
add_index "cards", ["awaken_type_id"], name: "index_cards_on_awaken_type_id"
|
||||||
|
|
Loading…
Reference in a new issue