我有两个模型product和product_category 一个产品包含或映射有多个产品类别,反之亦然。 它通过多对多关系进行维护,为此存在另一种称为products_in_category的模型。 我将Active Admin用于后端和CRUD,现在需要在Active Admin的“产品索引”页面中显示多个product_categories。 任何建议或帮助对我来说都是很好的。
#app/models/product.rb
class Product < ApplicationRecord
has_many :products_in_categories
has_many :product_categories, through: :products_in_categories, dependent: :destroy
accepts_nested_attributes_for :product_categories
end
#app/models/product_category.rb
class ProductCategory < ApplicationRecord
has_many :products_in_categories
has_many :products, through: :products_in_categories, dependent: :destroy
accepts_nested_attributes_for :products
accepts_nested_attributes_for :products_in_categories
end
#app/models/products_in_category.rb
class ProductsInCategory < ApplicationRecord
belongs_to :product_category
belongs_to :product
end
答案 0 :(得分:1)
您可以在列中列出类别名称(只需将name
更改为您的实际属性即可)
index do
# other columns goes here
column('Categories') { |p| p. product_categories.pluck(:name).join(', ') }
end