小编Ger*_*ine的帖子

在Active Admin中选择"has_many through"关联

我已经看到了几个同样问题的问题,例如

通过Active Admin使用HABTM或Has_many

但我仍然在努力让事情发挥作用(此时我已尝试过多种方式).

我的模型(稍微复杂于用户模型的'技术员'别名):

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
Run Code Online (Sandbox Code Playgroud)

我已经为AnalysisType模型注册了一个ActiveAdmin模型,并且希望能够选择(已经创建)技术人员以在下拉列表/复选框中与该AnalysisType相关联.我的ActiveAdmin设置目前看起来像:

ActiveAdmin.register AnalysisType do
  form do |f|
    f.input :analysis_type_technicians, as: :check_boxes, :collection => User.all.map{ …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails associations has-many-through activeadmin

2
推荐指数
1
解决办法
4890
查看次数