获取作者的所有评论。使用Ecto模型进行二次连接

时间:2017-06-22 23:03:00

标签: elixir ecto

我有三个与此类似的模型

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

如何为作者创建一个显示所有评论的字段或方法?基本上是次要连接。我需要能够以某种方式将它暴露给Absinthe模式。

1 个答案:

答案 0 :(得分:1)

使用has_many :through

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