Ruby On Rails Active Admin has_many下拉列表添加新对象

时间:2017-06-02 12:48:57

标签: ruby-on-rails ruby activeadmin

我有一个带有自引用has_many关系的模型,虽然是另一个模型。 所以我有模型Product,每个产品都可以有其他产品。

class Project < ActiveRecord::Base
  has_many :project_connections
  has_many :related_projects, through: :project_connections,
                              dependent: :destroy
  accepts_nested_attributes_for :project_connections,
                                :related_projects, allow_destroy: true

  ...
end

和我的ProjectConnection模型:

class ProjectConnection < ActiveRecord::Base
  belongs_to :project
  belongs_to :related_project, class_name: Project

  accepts_nested_attributes_for :project
  accepts_nested_attributes_for :related_project

  ...
end

在Active Admin中我想在创建/编辑项目视图中删除或添加related_project。 在admin/project.rb

form do |f|
    inputs 'Продолжение проекта' do
      f.has_many :related_projects, heading: 'Добавьте проект', allow_destroy: true, new_record: true  do |i|
        i.input :id, as: :select, collection: Project.all, include_blank: false
      end
    end
end

http://localhost:3000/admin/projects/1/edit enter image description here enter image description here

添加新的related_project后按“更新项目”,没有任何更改。 那我该怎么做才能解决这个问题呢?感谢。

2 个答案:

答案 0 :(得分:1)

一个可能的解决方案是在项目上有一个抽象字段,connected_project_ids和一个before_save过滤器来保存更改,然后用一个后加载来填充字段以初始化表单

before_save   :connect_projects
after_find    :set_connected_project_ids
attr_accessor :connected_project_ids

def set_connected_project_ids
  self.connected_project_ids = connected_projects.pluck(:related_project_id)
end

def connect_projects
  connected_ids = connected_projects.pluck(:related_project_id)

  #projects that are in connected_project_ids, but not yet associated
  projects_to_connect = connected_project_ids - connected_ids
  projects_to_connect.each do |cp_id|
    connected_projects.create(related_project_id: cp_id) 
  end

  #projects that are associated, but not in connected_project_ids
  projects_to_disconnect = connected_ids - connected_project_ids
  projects_to_disconnect.each do |cp_id|
    connected_projects.where(related_project_id: cp_id).destroy_all
  connected_ids
end

这将允许您通过在connected_project_ids字段中提交现在应该与之关联的项目ID数组来添加和删除关联的项目。

答案 1 :(得分:0)

所以,我自己解决了问题。由于ProjectConnection存储项目{id,project_id,related_project_id}之间的连接,因此使用projects.project_connections进行操作是正确的,而不是projects.related_projects。首先,我在projects.project_connections中为admin/project.rb声明了一个params结构:

project_connections_attributes: [
  :id,
  :project_id,
  :related_project_id,
  :_destroy
]

然后代替

inputs 'Продолжение проекта' do
  f.has_many :related_projects, heading: 'Добавьте проект', allow_destroy: true, new_record: true  do |i|
    i.input :id, as: :select, collection: Project.all, include_blank: false
  end
end

我写了

inputs 'Продолжение проекта' do
  f.has_many :project_connections, heading: 'Добавьте проект', allow_destroy: true, new_record: true  do |i|
    i.input :related_project_id, as: :select, collection: Project.all, include_blank: false
    i.input :project_id, :input_html => { :value => f.object.id }, as: :hidden
  end
end

效果很好)