Ecto seems to support polymorphic association as I read through https://github.com/elixir-lang/ecto/issues/389 and its related issues linked from it.
Let's say I need a Comment model association on Task and Event models. If my understanding of Ecto association with custom source is right, then we need four tables and three models,
Tables
- tasks
- events
- tasks_comments
- events_comments
Model
- Task
- Event
- Comment
Task and Event model will have the has_many association with custom source as below.
defmodule ExampleApp.Task do
use ExampleApp.Web, :model
schema "tasks" do
field :title, :string
field :body, :string
has_many :comments, {"tasks_comments", Comment}
timestamps
end
end
defmodule ExampleApp.Event do
use ExampleApp.Web, :model
schema "events" do
field :title, :string
field :body, :string
has_many :comments, {"events_comments", Comment}
timestamps
end
end
Now what I don't understand is how should the Comment model look like?
How does the Comment model handle two table? and How does it handle belongs_to association to the different models?