ssomnoremac
ssomnoremac

Reputation: 759

Get all Comments for Author. Secondary join using Ecto Models

I have three models that are similar to this

schema "Author" do
  has_many :posts, Repo.Post
end

schema "Post" do
  has_many :comments Repo.Post
  belongs_to :author, Repo.Author
end

schema "Comment" do
  belongs_to :post, Repo.Post
end

how do I create a field or method for Author to display all comments? Essentially a secondary join. I need to be able to expose this to an Absinthe schema somehow.

Upvotes: 0

Views: 97

Answers (1)

Mike Buhot
Mike Buhot

Reputation: 4885

Use has_many :through

schema "Author" do
  has_many :posts, Repo.Post
  has_many :comments, through: [:posts, :comments]
end

Upvotes: 1

Related Questions