magnusvk/counter_culture

counter_culture_fix_counts on has_many association results in error

Closed this issue · 3 comments

I have a counter on a has_many relationship and it works perfectly fine. However, when I run the CollectionsContent.counter_culture_fix_counts command I get the following error:

Content Load (1.0ms)  SELECT  "contents".id, "contents".id, COUNT("collections_contents".)*1 AS count, "contents".contents_count FROM "contents" LEFT JOIN "collections_contents" AS collections_contents ON "contents".id = collections_contents.collection_id GROUP BY "contents"."id" ORDER BY "contents"."id" ASC LIMIT $1  [["LIMIT", 1000]]
Traceback (most recent call last):
        1: from (irb):1
ActiveRecord::StatementInvalid (PG::SyntaxError: ERROR:  syntax error at or near ")")
LINE 1: ...".id, "contents".id, COUNT("collections_contents".)*1 AS cou...
                                                             ^
: SELECT  "contents".id, "contents".id, COUNT("collections_contents".)*1 AS count, "contents".contents_count FROM "contents" LEFT JOIN "collections_contents" AS collections_contents ON "contents".id = collections_contents.collection_id GROUP BY "contents"."id" ORDER BY "contents"."id" ASC LIMIT $1

Classes

class Collection < ApplicationRecord
  has_many :collections_contents
  has_many :contents, through: :collections_contents, dependent: :destroy
end

class Content < ApplicationRecord
  has_many :collections_contents
  has_many :collections, through: :collections_contents, dependent: :destroy
end

class CollectionsContent < ActiveRecord::Base
  belongs_to :content
  belongs_to :collection
  counter_culture :collection, column_name: :contents_count
end

Is there something wrong with the code?

Perhaps that model doesn't have a primary key set? That's what the reconciler uses.

I'm happy to help dig on this more, but you'd have to open a pull request with a failing test case for me to be able to reproduce this.

Thanks for the response, @magnusvk. You are absolutely correct, the model didn't have a primary key which caused the error. Thanks for all of your hard work

Only want to add the corresponding migration for that issue, to save time for others:

add_column :collections_contents, :id, :primary_key

BTW: Thank you for this gem 👍