In my Case
model
has_many :case_products, dependent: :destroy
has_many :products, through: :case_products
in my Product
model
class Product < ActiveRecord::Base
has_many :cases
end
in CaseProduct
class CaseProduct < ActiveRecord::Base
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 "case" 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
PG::UndefinedColumn: ERROR: column cases.product_id does not exist
LINE 1: SELECT COUNT(*) FROM "cases" WHERE "cases"."product_id" = $1
^
: SELECT COUNT(*) FROM "cases" WHERE "cases"."product_id" = $1
You must set associations in Product like your Case model:
class Product < ActiveRecord::Base
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.