How to implement many-to-many in ActiveAdmin?

There are many-to-many:

class Employee < ActiveRecord::Base
    has_many :employees_and_positions
    has_many :employees_positions, through: :employees_and_positions
end

class EmployeesAndPosition < ActiveRecord::Base
    belongs_to :employee
    belongs_to :employees_position
end

class EmployeesPosition < ActiveRecord::Base
    has_many :employees_and_positions
    has_many :employees, through: :employees_and_positions
end

How to implement the choice (check_boxes) of the position in the form when adding an employee? I wrote this option:

f.inputs 'Communications' do
    f.input :employees_positions, as: :check_boxes
end

It displays a list of positions in the form, but does not save anything in the table (employees_and_positions). How to fix?

+4
source share
1 answer

Suppose you have one employee, you can refer to association identifiers employees_positionsusing employee.employees_position_ids. Accordingly, you can massage previously assigned EmployeesPositionobjects with check_box for each EmployeesPosition, but you need to use the attributeemployee_position_ids

= f.input :employee_position_ids, as: :check_boxes, collection: EmployeesPosition.all

, , employee_position_ids :

ActiveAdmin.register Employee do
  permit_params employee_position_ids: []
end

http://activeadmin.info/docs/2-resource-customization.html

+4

All Articles