active admin many to many show index PG::UndefinedColumn: ERROR: column cases.product_id does not exist
active admin many to many show index PG::UndefinedColumn: ERROR: column cases.product_id does not exist
在我的 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" =
^
: SELECT COUNT(*) FROM "cases" WHERE "cases"."product_id" =
您必须像案例模型一样在产品中设置关联:
class Product < ActiveRecord::Base
has_many :case_products, dependent: :destroy
has_many :cases, through: :case_products
end
如果您只使用 has_many :cases
,Rails 假设案例模型有一个 product_id
列。
在我的 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" =
^
: SELECT COUNT(*) FROM "cases" WHERE "cases"."product_id" =
您必须像案例模型一样在产品中设置关联:
class Product < ActiveRecord::Base
has_many :case_products, dependent: :destroy
has_many :cases, through: :case_products
end
如果您只使用 has_many :cases
,Rails 假设案例模型有一个 product_id
列。