1

I use counter_culture gem, but when I want to use it, it throws an error that it cannot find product_id field in Product model, which makes sense because it should look for 'id'.

Below is the query generated when I call 'Product.counter_culture_fix_counts'. As you can see, it produced 'LEFT JOIN products AS products ON product_categories.id = products.product_id', which is not correct and it should be 'LEFT JOIN products AS products ON product_categories.product_id = products.id'.

SELECT  categories.id, categories.id, COUNT(products.id) AS count, categories.products_count FROM "categories" LEFT JOIN product_categories AS product_categories ON categories.id = product_categories.category_id LEFT JOIN products AS products ON product_categories.id = products.product_id AND (products.active = 't') GROUP BY "categories"."id"  ORDER BY "categories"."id" ASC LIMIT 1000 OFFSET 0

And here are the models:

# Product Model

class Product
   has_one :product_category, dependent: :destroy
   has_one :category, :through => :product_category

   counter_culture [:product_category, :category], :column_name => Proc.new { |product| 'products_count' }, :column_names => { ["products.active = ?", true] => 'products_count' }, :touch => true
end

# ProductCategory Model

class ProductCategory
   belongs_to :product
   belongs_to :category
end

# Category Model

class Category
   has_many :product_categories, dependent: :destroy
   has_many :products, :through => :product_categories
end

Any idea how to get this working? Thank you, Miro

Miroslav
  • 158
  • 2
  • 22

0 Answers0