I’m trying to model a simple situation where there are two different many_to_many associations between two models Book and Person.
Person is an author of a Book
Person is mentioned in a Book
I would like to use two intermediary tables authors_books and mentioned_in_books with foreign keys person_id and book_id but cannot figure out how to define two different many_to_many associations pointing to the same model in one schema.
Check part about many_to_many with join_through parameter
defmodule Post do
use Ecto.Schema
schema "posts" do
many_to_many :comments, Comment, join_through: "posts_comments"
end
end
defmodule Task do
use Ecto.Schema
schema "tasks" do
many_to_many :comments, Comment, join_through: "tasks_comments"
end
end
Yes, that’s what I have figured out already. The point is how I could have something like
defmodule Post do
use Ecto.Schema
schema "posts" do
many_to_many :comments, Comment, join_through: "posts_favourite_comments"
many_to_many :comments, Comment, join_through: "posts_clever_comments"
end
end
defmodule Task do
use Ecto.Schema
schema "tasks" do
many_to_many :comments, Comment, join_through: "tasks_favourite_comments"
many_to_many :comments, Comment, join_through: "tasks_clever_comments"
end
end
Sure, however then you should map FavouriteComment and CleverComment modules to get data from all four tables and trough those get real comments. Because those tables only contains comment_id.