2

I've seen several questions asked about this along the same vein, e.g.

Using HABTM or Has_many through with Active Admin

but I'm still struggling to get things to work (I've tried multiple ways at this point).

My models (slightly complicated by the 'technician' alias for the user model):

class AnalysisType < ActiveRecord::Base
  has_many :analysis_type_technicians, :dependent => :destroy
  has_many :technicians, :class_name => 'User', :through => :analysis_type_technicians
  accepts_nested_attributes_for :analysis_type_technicians, allow_destroy: true
end

class User < ActiveRecord::Base
  has_many :analysis_type_technicians, :foreign_key => 'technician_id', :dependent => :destroy
  has_many :analysis_types, :through => :analysis_type_technicians
end

class AnalysisTypeTechnician < ActiveRecord::Base
  belongs_to :analysis_type, :class_name => 'AnalysisType', :foreign_key => 'analysis_type_id' 
  belongs_to :technician, :class_name => 'User', :foreign_key => 'technician_id'
end

I have registered an ActiveAdmin model for the AnalysisType model and want to be able to select (already created) Technicians to associate with that AnalysisType in a dropdown/checkbox. My ActiveAdmin setup currently looks like:

ActiveAdmin.register AnalysisType do
  form do |f|
    f.input :analysis_type_technicians, as: :check_boxes, :collection => User.all.map{ |tech|  [tech.surname, tech.id] }
    f.actions
  end 

  permit_params do
    permitted = [:name, :description, :instrumentID, analysis_type_technicians_attributes: [:technician_id] ]
    permitted
  end

end  

Whilst the form seems to display okay, the selected technician does not get attached upon submitting. In the logs I'm getting an error 'Unpermitted parameters: analysis_type_technician_ids'.

I've tried multiple ways of doing this following advice in other related SO pages but am always coming up against the same issue, i.e. unpermitted parameterd of some nature. Can anyone point out what I am doing wrong? (I'm using Rails 4 by the way)

Community
  • 1
  • 1
GerryDevine
  • 111
  • 1
  • 9

1 Answers1

21

Managing the association via has_and_belongs_to_many or has_many relations does not require the use of accepts_nested_attributes_for. This type of form input is managing the Technician IDs associated with the AnalysisType record. Defining your permitted parameters and form like the following should allow those associations to be created.

ActiveAdmin.register AnalysisType do
  form do |f|
    f.input :technicians, as: :check_boxes, collection: User.all.map { |tech| [tech.surname, tech.id] }
    f.actions
  end

  permit_params :name, :description, :instrumentID, technician_ids: []
end

In the case where the creation of new Technician records is required that is when the accepts_nested_attributes_for would be used.


Note: Updated answer to match comments.

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
Charles Maresh
  • 3,323
  • 22
  • 29
  • I think your suggestion is how I actually started out. But having tried it again I'm still getting an 'unpermitted parameters' error on the :analysis_type_technicians_id. The following is being returned from the form: "analysis_type"=>{"name"=>"fsdfds", "instrumentID"=>"fsdfsdfds", "description"=>"fsdfds", "analysis_type_technician_ids"=>["", "1", "2"]}, "commit"=>"Update Analysis type", "id"=>"2"}. Am I using the permit_params in the wrong way? Or do I need to put this these at the controller/model level too? – GerryDevine Jan 15 '15 at 00:29
  • You're right. Changing the permitted_params to `:analysis_type_technician_ids => []` for the that field should do the trick. – Charles Maresh Jan 15 '15 at 16:00
  • Wait, shouldn't we be targeting technicians directly, i.e. 'f.input :technicians......' along with 'permit_params......, :technician_ids'? When I do that things work fine, otherwise it complains that it can't find the :analysis_type_technician_ids (which I think makes sense). – GerryDevine Jan 15 '15 at 22:11
  • That should work if the `:through` relation is simple and records can be created via the `technicians` relation. Occasionally there are complication that require more work, such as validations, that prevent records from being created through a `:through` relation. – Charles Maresh Jan 16 '15 at 15:51