Skip to content

Usage with STI table #438

@mbajur

Description

@mbajur

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:

image

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:

  1. article.hash_tree - this produces SELECT "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 as ancestor_id in DB is 213 not 4
  2. article.replies.hash_tree - this returns only 0 generation of records, so just comment is being returned in the hash result. No comment2 as comment child.
  3. article.children.hash_tree - nothing is returned, same query as in 1

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! 🙏

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions