ruby-on-rails - active admin many to many show index PG::UndefinedColumn: ERROR: column cases.product_id does not exist
问题描述
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.
推荐阅读
- reactjs - Material UI - 将道具应用于伪元素
- amazon-web-services - AWS Route53 子域间歇性关闭
- mongodb - 添加更多属性时未使用 Mongo 索引
- html - 材质图标对齐问题
- html - 如何从材料设计概述的文本字段中删除占位符
- java - 无论如何通过意图在android studio中打开秘密网络设置?
- c# - VS Code 无法在 unity3D 中捕获断点
- linux - 使用 Electron Builder 构建 appImage 时出现“不允许符号链接操作”错误
- android - setGravity 方法是无操作的 - 当在文本 toasts 上调用时
- ios - 如何在 CollectionView 的组合布局中更改选择颜色