如何在rails admin中显示关联的名称? (而不是id)
问题描述:
我有一个Editor
模型与has_many :categories
和has_many :types
,通过表categories_editors
和editors_types
。如何在rails admin中显示关联的名称? (而不是id)
在管理界面中,我希望看到categories
的名称。它适用于types
(请参见下图),但是这两种关联的定义方式都是相同的。
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
是否有人有想法?
答
为了使您的对象的名称显示,你必须重命名列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