ActiveAdmin和Rails 4的多个HABTM属性:未保存数据

时间:2014-05-22 16:39:10

标签: ruby-on-rails ruby-on-rails-4 activeadmin activemodel

我有以下型号:

class Programme < ActiveRecord::Base

  has_and_belongs_to_many :nationalities, class_name: 'Nation', join_table: 'nationalities_nations'
  has_and_belongs_to_many :destinations, class_name: 'Nation', join_table: 'destinations_nations'

  accepts_nested_attributes_for :nationalities
  accepts_nested_attributes_for :destinations

end

class Nation < ActiveRecord::Base

  has_and_belongs_to_many :nationality_programmes, class_name: 'Programme', join_table: 'nationalities_nations'
  has_and_belongs_to_many :destination_programmes, class_name: 'Programme', join_table: 'destinations_nations'

  accepts_nested_attributes_for :nationality_programmes
  accepts_nested_attributes_for :destination_programmes

end

在主动管理员中,我有以下配置,可以正确预先选择任何现有的存储国家/地区引用(参见屏幕截图)。

ActiveAdmin.register Programme do

  permit_params :title,
            destinations_ids: [:id],
            nationalities_ids: [:id]


  form do |f|
    f.actions
    f.inputs 'Countries / Regions' do
      f.input :nationalities, :as => :select, :input_html => {:multiple => true}
      f.input :destinations, :as => :select, :input_html => {:multiple => true}
      f.input :title
    end
    f.actions
  end
end

但是,当我选择其他国家/地区时,表单会成功保存,但不会存储引用。

这是我的架构:

ActiveRecord::Schema.define(version: 20140522131219) do

  create_table "destinations_nations", force: true do |t|
    t.integer "programme_id", null: false
    t.integer "nation_id",    null: false
  end

  create_table "levels_programmes", force: true do |t|
    t.integer "programme_id", null: false
    t.integer "level_id",     null: false
  end

  create_table "nationalities_nations", force: true do |t|
    t.integer "programme_id", null: false
    t.integer "nation_id",    null: false
  end

  create_table "nations", force: true do |t|
    t.string   "slug",       limit: 2
    t.string   "name"
  end

  create_table "programmes", force: true do |t|
    t.string   "title"
  end

end

enter image description here

更新:在active_admin#3196上交叉发布此问题,现已关闭,感谢Gregorio的帮助。

1 个答案:

答案 0 :(得分:20)

我通过改变

使其成功
permit_params :title,
        destinations_ids: [:id],
        nationalities_ids: [:id]

permit_params :title,
        destination_ids: [],
        nationality_ids: []