在我的Case
模型中
has_many :case_products, dependent: :destroy
has_many :products, through: :case_products
在我的Product
模型中
class Product < ActiveRecord::Base
has_many :cases
end
在 CaseProduct
class CaseProduct < ActiveRecord::Base
belongs_to :case
belongs_to :product
end
如何显示每个产品有多少箱?
在 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
我得到这个错误
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
您必须像案例模型一样在Product中设置关联:
class Product < ActiveRecord::Base
has_many :case_products, dependent: :destroy
has_many :cases, through: :case_products
end
如果仅使用has_many :cases
,Rails假设Case模型有一个product_id
列。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句