无法在 Rails Active Admin Index 页面中显示多个属性(嵌套项)
Can not show multiple attributes (nested items) in Rails Active Admin Index page
我有两个模型产品和 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
您可以在一列中列出类别名称(只需 name
更改为您的实际属性):
index do
# other columns goes here
column('Categories') { |p| p. product_categories.pluck(:name).join(', ') }
end
我有两个模型产品和 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
您可以在一列中列出类别名称(只需 name
更改为您的实际属性):
index do
# other columns goes here
column('Categories') { |p| p. product_categories.pluck(:name).join(', ') }
end