How to implement Subcategory pattern with Ecto?
I want to have comments field for separate entities via has_many
association, maybe with :through
option. Is it possible?
According to this great StackOverflow I’ve created entities
table and comments
table which relates to entity_id
. Then in my main tables like posts
and performers
I create entity_id
field.
I’m wondering how to associate post with comments and how to automatically create entity for post on creation.
My schema in short:
# Better to get rid of entity schema if its possible
schema "entities" do
has_many :comments, Comment
end
schema "comments" do
field :content, :string
belongs_to :manager, Manager
belongs_to :entity, Manager
end
schema "posts" do
field :title, :string
field :content, :string
belongs_to :entity, Entity # I'm trying to get rid of this
has_many :comments, through: [:entities, :comments] # Want to have something like this
end
schema "performers" do
field :name, :string
belongs_to :entity, Entity # I'm trying to get rid of this
has_many :comments, through: [:entities, :comments] # Want to have something like this
end
Also I can’t find a way to make factory seeds with this db design for post_comment
with ExMachina.Ecto
because entity
table has no relation to post
or performer
.
Is the best way to have multiple tables like post_comments
and performer_comments
or even separate tables for each entity comments?