我的组织安排为树。我有一个请求,要求为树中的不同根提供不同的命名方案。一个用户可能想要在“Region”上面命名为“Corporate”的树的顶层,然后是“Division”,而另一个用户可能想要在“All”下面有“Sections”。每个人都有自己想要在表格上显示的内部术语。
有很多宝石可以简单地管理层次关系,我认为它们中的任何一个都不一定更好或更差。我是rails的新手,来自更沉重的SQL背景。编写查询以获得我想要的内容非常简单,特别是在跟踪节点的深度时,但这似乎不像rails方式。我有一个想法,我可以在组织表中维护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