2

I have the following class with several counter_culture counters for the same association:

class Book < ::ActiveRecord::Base
  belongs_to :user

  counter_culture :user
  counter_culture :user, column_name: Proc.new { |b| b.comedy? ? 'comedy_books_count' : nil }
  counter_culture :user, column_name: Proc.new { |b| b.hard_cover? ? 'hard_cover_books_count' : nil }
end

The issue I have with this is that it executes the updates using several queries:

UPDATE "users" SET "books_count" = COALESCE("books_count", 0) + 1 WHERE "users"."id" = $1  [["id", 1616]]
UPDATE "users" SET "comedy_books_count" = COALESCE("comedy_books_count", 0) + 1 WHERE "users"."id" = $1  [["id", 1616]]
UPDATE "users" SET "hard_cover_books_count" = COALESCE("hard_cover_books_count", 0) + 1 WHERE "users"."id" = $1  [["id", 1616]]

Is there a way to do this more efficiently with counter_culture by executing the updates in a single query?

mrstif
  • 2,736
  • 2
  • 27
  • 28

1 Answers1

0

Each line is detected as a special case, it would be difficult for them to know which one they can optimise and which one they cannot. So I guess no.

borisrorsvort
  • 907
  • 7
  • 22