I am looking for a beautiful way to update a counter cache of a given model.
Here are my model:
class GameParticipation < ActiveRecord::Base
belongs_to :game, counter_cache: true
end
And:
class Game < ActiveRecord::Base
has_many :game_participations
end
Isn't it something better than iterating on each element like the following code ?
Game.ids.each {|id| Game.reset_counters(id, :game_participations) }
Instead of counting the number of responses every time the articles are displayed, a counter cache keeps a separate response counter which is stored in each article's database row. The counter updates whenever a response is added or removed.
For articles that predate the counter cache, the counter will be out of sync, as it's 0 by default. We can “reset” a counter for an object by using the .reset_counters method on it and passing the object's ID and the relation the counter should be updated for.
Clear the Cache for Analysis Services Models 1 Get the object identifierIn Management Studio, right-click an object, select Properties, and copy the value... 2 Run the query See More....
The counter updates whenever a response is added or removed. This allows the article index to render with one database query, without needing to join the responses in the query. To set it up, flip the switch in the belongs_to relation by setting the counter_cache option. This requires a field to the Article model named responses_count.
To update all the counter cache in one request I found inspiration on http://ryan.mcgeary.org/2016/02/05/proper-counter-cache-migrations-in-rails/
It can be done in one request with SQL :
ActiveRecord::Base.connection.execute <<-SQL.squish
UPDATE games
SET game_participations_count = (SELECT count(1)
FROM game_participations
WHERE game_participations.game_id = games.id)
SQL
It takes much less time to execute since all the updating is done in one request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With