closure_tree icon indicating copy to clipboard operation
closure_tree copied to clipboard

Hierarchy model inheritance breaks usage with STI and superclass validations

Open bb opened this issue 2 years ago • 1 comments

I was not able to rebuild! the hierarchy in my project after upgrading from 7.3 to 7.4 and found out this is due to the changes of #384.

My models look roughly like this:

class BasicItem < ApplicationRecord
  belongs_to :tenant
end

class HierarchicalItem < BasicItem
  belongs_to :parent, optional: true
  has_closure_tree order: "sort_order", numeric_order: true
end

class SpecificItemA < HierarchicalItem
end
class SpecificItemB < HierarchicalItem
end

In version 7.3 the HierarchicalItemHierarchy(ancestor_id: integer, descendant_id: integer, generations: integer) was extending AR::Base but now in 7.4 it's extending BasicItem which leads to the following issues:

  1. The hierarchy class and table it do not have a tenant_id, it never sets a tenant (and shouldn't), but it's validated to have one when filling the Hierarchy table
  2. it generates invalid SQL when commenting out belongs_to :tenant (as a workaround for 1.) and rebuilding: HierarchicalItemHierarchy Create (5.1ms) INSERT INTO "basic_item_hierarchies" ("ancestor_id", "descendant_id", "generations") VALUES ($1, $2, $3) RETURNING "id" [["ancestor_id", 5653], ["descendant_id", 5653], ["generations", 0]] -- the issue here is "RETURNING ID" because from BasicItem, it infers this would be the primary key while it isn't.

I think #384 should be reverted and another solution should be found for that.

bb avatar Dec 08 '21 12:12 bb

#395 seems to be the same issue, so I guess they can be solved together.

bb avatar Jul 04 '22 18:07 bb