0

I have written the necessary code including the join table to create the many to many relationship between category and products and it works from testing in the rails console. When I chose more than one category field it saves them and stays there when I go back to edit it but when I view the product the default number is set.

I tried using

permit_params categories_ids: []

But it still wouldn't set the multiple chosen categories when I saved the product.

category.rb

has_ancestry
has_many :children, class_name: 'Category', foreign_key: 'ancestry'

has_and_belongs_to_many :products
accepts_nested_attributes_for :products, allow_destroy: true

product.rb

has_and_belongs_to_many :categories
accepts_nested_attributes_for :categories, allow_destroy: true

products.rb (ActiveAdmin)

ActiveAdmin.register Product do
menu parent: "Shop"

scope :active, default: true
scope :disabled

filter :user
filter :categories
filter :personalisation
filter :name
filter :description
filter :out_of_stock
filter :price_cents
filter :weight

index do
    selectable_column
    column :name
    column :price
    column :description
    column :categories

    actions
end

form do |f|

  f.inputs "Details" do
    f.input :categories, as: :select, collection: Category.all, multiple: true   
  end

0 Answers0