Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a many-to-many in ActiveAdmin?

There a 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 a choice (check_boxes) positions in the form when adding an employee? I wrote this variant:

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 nothing to the table (employees_and_positions). How to fix?

like image 975
Colibri Avatar asked May 05 '16 11:05

Colibri


1 Answers

Suppose you have an employee, you can reference the ids of the employees_positions association by using employee.employees_position_ids. Accordingly, you can mass assign pre-existing EmployeesPosition objects using a check_box for each EmployeesPosition, but you need to use the employee_position_ids attribute"

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

Also, make sure you've whitelisted the employee_position_ids param in your active admin resource:

ActiveAdmin.register Employee do
  permit_params employee_position_ids: []
end

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

like image 100
Anthony E Avatar answered Sep 20 '22 20:09

Anthony E