Ecto association with a condition

前端 未结 2 1560
情书的邮戳
情书的邮戳 2021-02-19 01:55

Let\'s say I have two models, Post and Comment and the comment model can be 1 out of 2 types, normal and fancy which is defin

2条回答
  •  清歌不尽
    2021-02-19 02:40

    Conditional associations are now available in Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#has_many/3-filtering-associations

    defmodule Post do
      use Ecto.Schema
    
      schema "posts" do
        has_many :public_comments, Comment, where: [public: true]
      end
    end
    

提交回复
热议问题