GabeStah / vcp

0 stars 0 forks source link

Implement squeel gem to help with sorting issue? #82

Closed GabeStah closed 9 years ago

GabeStah commented 9 years ago

https://github.com/activerecord-hackery/squeel

GabeStah commented 9 years ago

counter_cache is actually the proper solution.

GabeStah commented 9 years ago
GabeStah commented 9 years ago

Using custom_counter_cache gem.

For characters#index Raid counter:

Ensure :raids association to Character is unique and define :raids_count counter_cache.

class Character < ActiveRecord::Base
  has_many :raids, -> {uniq}, through: :participations, dependent: :delete_all

  define_counter_cache :raids_count do |character|
    character.raids.count
  end
end

Since association is through Participation, add update_counter_cache call:

class Participation < ActiveRecord::Base
  update_counter_cache :character, :raids_count
end
GabeStah commented 9 years ago

Fixed by 4a12709d48e35375943c3bdc8463d5a764bfeb6c