如何在rails admin中显示关联名称? (而不是id)

时间:2017-04-23 14:05:01

标签: ruby-on-rails rails-admin

通过表格Editorhas_many :categories,我有一个has_many :types模型categories_editorseditors_types

在管理界面中,我想看到categories的名称。它适用于types(参见下图),但两种关联的定义方式相同。

enter image description here

class Editor < ApplicationRecord
  has_many :categories_editors
  has_many :categories, through: :categories_editors
  has_many :editors_types
  has_many :types, through: :editors_types
end

class Type < ApplicationRecord
  has_many :editors_types
  has_many :editors, through: :editors_types
end

class Category < ApplicationRecord
  has_many :categories_editors
  has_many :editors, through: :categories_editors
end

class CategoriesEditor < ApplicationRecord
  belongs_to :editor
  belongs_to :category
end

class EditorsType < ApplicationRecord
  belongs_to :editor
  belongs_to :type
end

有人有想法吗?

1 个答案:

答案 0 :(得分:0)

要显示对象的名称,必须重命名列name

所以我改变了:

  create_table "categories", force: :cascade do |t|
    t.string   "category"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

到:

  create_table "categories", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end