2017-08-24 75 views
0

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

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 

内主动联系我用宝石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 

在创建ProfessionAllocation正常工作与标签采集,编辑视图使得问题,因为它显示ProfessionAllocations,而不是相关专业。 我的问题是,我怎样才能在标签收集中显示相关的职业?

最佳 亚历

回答

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