带有联接表的活动管理员标记集合

时间:2017-08-24 11:18:53

标签: ruby tags foreign-keys activeadmin

我在Active Admin Tag集合中显示正确的值时遇到问题。

enter image description here

为了建立多对多关系,我创建了一个连接表:

  create_table "profession_allocations", force: :cascade do |t|
    t.integer "master_id"
    t.integer "slave_id"
    t.index ["master_id"], name: "index_profession_allocations_on_master_id", using: :btree
    t.index ["slave_id"], name: "index_profession_allocations_on_slave_id", using: :btree
  end

和模特:

class ProfessionAllocation < ApplicationRecord

  has_and_belongs_to_many :master, :class_name => 'Profession'
  has_and_belongs_to_many :slave, :class_name => 'Profession'

end

与我的职业表建立关系:

create_table "professions", force: :cascade do |t|
    t.string   "kind",                                   null: false
    t.string   "list",                                   null: false
    t.datetime "created_at",                             null: false
    t.datetime "updated_at",                             null: false
    t.string   "summary_de", default: "summary german",  null: false
    t.string   "summary_en", default: "summary english", null: false
  end

和模型:

class Profession < ApplicationRecord

  validates_presence_of :kind, :list
  validates  :kind, presence: true, uniqueness: true
  validates  :list, presence: true
  validates  :summary_de, presence: true
  validates  :summary_en, presence: true

  has_many :master_profession_allocations, :class_name => 'ProfessionAllocation', :foreign_key => 'master_id'
  has_many :slave_profession_allocations, :class_name => 'ProfessionAllocation', :foreign_key => 'slave_id'

end

在Active Admin中我使用gem activeadmin_addons 来使用标签,但我想这对于这个问题并不惹麻烦。

    if f.object&.persisted?
          f.input :master_profession_allocations, as: :tags, collection: Profession.where.not(id: f.object.id).order('summary_en ASC'), display_name: :summary_en
        else
          f.input :master_profession_allocations, as: :tags, collection: Profession.all.order('summary_en ASC'), display_name: :summary_en
        end
end

虽然使用Tag集合创建ProfessionAllocation工作正常,但编辑视图会产生问题,因为它显示的是ProfessionAllocations而不是相关的Professions。 我的问题是,如何在标签集中显示相关的专业?

最佳

亚历

1 个答案:

答案 0 :(得分:1)

我通过创建一个小节来解决它。这使我也可以在分配中存储附加数据:

f.inputs do
        f.has_many :master_profession_allocations, heading: 'Slaves', allow_destroy: true, new_record: true do |pa|
          if f.object&.persisted?
            pa.input :level, as: :select, collection: ProfessionAllocation::LEVELS
            pa.input :slave_id, as: :select, collection: Profession.joins('LEFT JOIN profession_allocations pa on professions.id = pa.master_id WHERE professions.id !='  + f.object.id.to_s).map { |pa| [pa.summary_en, pa.id] }
          else
            pa.input :level, as: :select, collection: ProfessionAllocation::LEVELS
            pa.input :slave_id, as: :select, collection: Profession.joins('LEFT JOIN profession_allocations pa on professions.id = pa.master_id').map { |pa| [pa.summary_en, pa.id] }
          end
        end
      end