如何在 rails admin 中显示协会名称? (而不是身份证)
How to show association's name in rails admin ? (instead of id)
我有一个 Editor
模型 has_many :categories
和 has_many :types
,通过表 categories_editors
和 editors_types
。
我想在管理界面中看到 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
我有一个 Editor
模型 has_many :categories
和 has_many :types
,通过表 categories_editors
和 editors_types
。
我想在管理界面中看到 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