评论
mizuhashi 发表于

ancestry 因为 parent children 都不是定义成关联的,没办法 includes,推荐 (closure_tree)[https://github.com/mceachen/closure_treegrandparent这种特定层级的关联也可以自己加:],性能会好很多,需要

has_one :grandparent, class_name: 'Catalog', through: :grandparent_hierarchy, source: :ancestor
has_one :grandparent_hierarchy, ->{ where(generations: 2) }, class_name: 'CatalogHierarchy', foreign_key: :descendant_id