Ecto association with a condition

前端 未结 2 1564
情书的邮戳
情书的邮戳 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:38

    Until recently, this wasn't available in Ecto. The other answer to this question provides the current details. There was a lengthy discussion about how to add it this GitHub issue.

    In older versions of Ecto, you could use a composable query for this:

    defmodule MyApp.Comment do
      
      ...schema, etc.
    
      def fancy(query) do
        from c in query,
          where: type == 0
      end
    
      def normal(query) do
        from c in query,
          where: type == 1
      end    
    end
    

    You can then use has_many :comments, MyApp.Comment and query based on that:

    assoc(post, :comments) |> Comment.fancy() |> Repo.all()
    

    Here is a blog post about composable queries.

    You can also use a preload with a query:

    fancy_query = from(c in Comments, where: type == 0)
    Repo.preload(post, comments: fancy_query)
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题