Description
Hey! First of all - big apologies if such issue was already created at some point. I'm having a hard time trying to even google for it as i'm unsure how to form my search query properly for this one. Anyway, i have a objects
table and two STI models using it
class Namespace::Object
has_many :replies, class_name: 'Comment', foreign_key: :in_reply_to_ap_object_id
has_closure_tree parent_column_name: :in_reply_to_ap_object_id
end
class Article < Namespace::Object
end
class Comment < Namespace::Object
end
So, basically, it's a one big tree structure with objects associated with each other using in_reply_to_ap_object_id
(which is a foreign key to either Article
or Comment
STI model, basically to Namespace::Object
model instance. And now, when I'm doing the following:
article = Article.create! # article.id = 4
comment = Comment.create! # comment.id = 213
comment2 = Comment.create!
article.add_child(comment)
comment.add_child(comment2)
a following record in hierarchies model is being created:

The problem is - for some reason unknown to me, the ancestor_id
is set to the comment id instead of the article id (213 instead of 4) which leads to me not being able to fetch article comments using hash_tree
method. I tried several ways of doing that but each has it's own quirks:
article.hash_tree
- this producesSELECT "activity_pub_objects".* FROM "activity_pub_objects" INNER JOIN "activity_pub_object_hierarchies" ON "activity_pub_objects"."id" = "activity_pub_object_hierarchies"."descendant_id" WHERE "activity_pub_object_hierarchies"."ancestor_id" = $1 ORDER BY "activity_pub_object_hierarchies".generations ASC [["ancestor_id", 4]]
query which obviously returns nothing asancestor_id
in DB is213
not4
article.replies.hash_tree
- this returns only 0 generation of records, so justcomment
is being returned in the hash result. Nocomment2
ascomment
child.article.children.hash_tree
- nothing is returned, same query as in1
I have a gut feeling it's related to the fact I'm using STI instead of having two separate models but that's a requirement of this specific app and i can't really change it.
PS. i just tested deep-nesting several comments and hash_tree
works perfectly fine starting from zero level comment - so for my example comment.hash_tree
returns a full comments tree as intented. It seems just using article
as a source of hash_tree
is breaking things
I would really appreciate any tips on this one! 🙏