Title: active admin many to many show index PG181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16UndefinedColumn: ERROR: column cases.product_id does not exist
Tags: ruby-on-rails;many-to-many;activeadmin
Question: In my ```Case``` model
```  has_many :case_products, dependent: :destroy
  has_many :products, through: :case_products
```
in my ```Product``` model
```class Product < ActiveRecor181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16Base
  has_many :cases
end
```
in ```CaseProduct```
```class CaseProduct < ActiveRecor181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16Base
  belongs_to :case
  belongs_to :product
end
```
how can I display how many cases I have for each product?
in ```active admin product.rb```
```ActiveAdmin.register Product do
  permit_params :id, :name ,case_ids: []
  index do
    column :id
    column :name
    column &quot;case&quot; do |m|
      m.cases.count
    end

    actions

  end
  
  show do
      attributes_table do
        row :id
        row :name
        row :case
        
      end 
  end

end
```
I got this error
```PG181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16UndefinedColumn: ERROR:  column cases.product_id does not exist
LINE 1: SELECT COUNT(*) FROM &quot;cases&quot; WHERE &quot;cases&quot;.&quot;product_id&quot; = $1
                                           ^
: SELECT COUNT(*) FROM &quot;cases&quot; WHERE &quot;cases&quot;.&quot;product_id&quot; = $1
```


Here is the accepted answer: You must set associations in Product like your Case model:
```class Product < ActiveRecor181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16Base
  has_many :case_products, dependent: :destroy
  has_many :cases, through: :case_products
end
```
If you only use ```has_many :cases```, Rails assumes Case model has a ```product_id``` column.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:12/13/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ruby-on-rails><many-to-many><activeadmin>', 'Title': 'active admin many to many show index PG::UndefinedColumn: ERROR: column cases.product_id does not exist', 'ViewCount': 80, 'id': 65271711, 'user_AboutMe': '<p>Hi, I am a Ruby on Rails web developer and I would love to help :)</p>\n<p><a href="https://www.paypal.com/donate?hosted_button_id=4PTP83QQLV98G" rel="nofollow noreferrer">https://www.paypal.com/donate?hosted_button_id=4PTP83QQLV98G</a></p>\n', 'user_AccountId': 8527822.0, 'user_DisplayName': 'nourza', 'user_Reputation': 2087.0, 'user_Views': 978.0, 'user_id': 6392696.0}}