2017-06-22 49 views
0

我有三種型號都與此類似獲取作者的所有評論。二級加入使用外生模型

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

使用has_many :through

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

我得到了'不匹配的右手邊值:%Ecto.Association.HasThrough'我想是因爲不像你所提供的鏈接,我的'posts'(二次表的例子)不屬於'評論'。換句話說,他們沒有'comment_id'。 – ssomnoremac