mardi 22 novembre 2016

STI with Polymorphism self referential

i have 4 models Group,Post,Comment and reply models

where comment and reply inherits from Post.every Group has_many comments. every comment has_many reply

my models like that

class Post < ApplicationRecord
     belongs_to :postable, polymorphic: true
end

group model

class Group < ApplicationRecord
  has_many :comments, as: :postable, class_name: "Comment"
end

comment model

class Comment < Post
  has_many :replies, as: :postable, class_name: "Reply"
end

i want to use Polymorphism with sti as follow

  1. postable_type refer to group as result of group-comment relationship

    postable_type refer to comment and equal 'comment' as result of comment-reply relationship

my problem is in the second relationship as always postable_type equal to STI base 'post' but it should to equal to 'comment' ?????.

How to create and query replies from comments?

Aucun commentaire:

Enregistrer un commentaire