我有按树排列的组织。我要求为树中的不同根设置不同的命名方案。一个用户可能希望在“Region”上方名为“Corporate”的树的顶层,然后是“Division”,而另一个用户可能希望在“All”下方有“Sections”。每个人都有自己的内部术语,他们希望在表格等上显示它们。
有很多宝石可以简单地管理等级关系,我认为它们中的任何一个都不一定更好或更坏。我是 Rails 的新手,来自更重的 SQL 背景。编写一个查询来获得我想要的东西非常简单,尤其是在跟踪节点的深度时,但这看起来不像是轨道方式。我有一个想法,我可以在 organizations 表中维护一个 organization_scheme_id,但是从那里将其与实际级别相关联并不那么清楚。
以下模式/模型假设我正在使用 Ancestry,但我至少没有与其中任何一个结婚。
class Organization < ActiveRecord::Base
has_ancestry :cache_depth => true, :orphan_strategy => :adopt, :depth_cache_column => :depth
validates :name, presence: true
has_one :organization_scheme
end
class OrganizationLevel < ActiveRecord::Base
validates :name, presence: true
has_one :organization_scheme
end
class OrganizationScheme < ActiveRecord::Base
belongs_to :organization
belongs_to :organization_level
end
create_table "organization_levels", id: false, force: true do |t|
t.uuid "id", null: false
t.string "name"
t.integer "depth"
t.uuid "organization_scheme_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "organization_levels", ["organization_scheme_id", "depth"], name: "index_organization_levels_on_organization_scheme_id_and_depth", using: :btree
create_table "organization_schemes", id: false, force: true do |t|
t.uuid "id", null: false
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "organizations", id: false, force: true do |t|
t.uuid "id", null: false
t.string "name"
t.string "string"
t.integer "depth"
t.string "ancestry"
t.uuid "organization_scheme_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "organizations", ["ancestry"], name: "index_organizations_on_ancestry", using: :btree
add_index "organizations", ["organization_scheme_id", "depth"], name: "index_organizations_on_organization_scheme_id_and_depth", using: :btree
create_table "organizations_users", id: false, force: true do |t|
t.uuid "user_id"
t.uuid "organization_id"
end
add_index "organizations_users", ["organization_id", "user_id"], name: "index_organizations_users_on_organization_id_and_user_id", using: :btree
add_index "organizations_users", ["user_id", "organization_id"], name: "index_organizations_users_on_user_id_and_organization_id", using: :btree