Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Elixir 与某种疾病的外胚层联系_Elixir_Phoenix Framework_Ecto - Fatal编程技术网

Elixir 与某种疾病的外胚层联系

Elixir 与某种疾病的外胚层联系,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,假设我有两个模型,Post和Comment,评论模型可以是两种类型中的一种,normal和fancy,这是由comments表中的type列定义的 现在我想在我的Post模型上添加两个关联,其中一个是指花哨的评论,另一个是指普通的评论,我该怎么做?所以我想要这样的东西: has_many :fancy_comments, MyApp.Comment, where: [type: 0] has_many :normal_comments, MyApp.Comment, where: [type:

假设我有两个模型,
Post
Comment
,评论模型可以是两种类型中的一种,
normal
fancy
,这是由
comments
表中的
type
列定义的

现在我想在我的
Post
模型上添加两个关联,其中一个是指花哨的评论,另一个是指普通的评论,我该怎么做?所以我想要这样的东西:

has_many :fancy_comments, MyApp.Comment, where: [type: 0]
has_many :normal_comments, MyApp.Comment, where: [type: 1]

直到最近,这还没有在外星。这个问题的另一个答案提供了当前的细节。关于如何添加它,进行了长时间的讨论

在较旧版本的EXTO中,您可以使用可组合查询:

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
然后,您可以使用
has\u many:comments、MyApp.Comment
并基于此进行查询:

assoc(post, :comments) |> Comment.fancy() |> Repo.all()
这是一篇关于我的博客

您还可以对查询使用预加载:

fancy_query = from(c in Comments, where: type == 0)
Repo.preload(post, comments: fancy_query)

条件关联现在可在
exto
中使用:


难道你不知道是否有可能实施,是否计划实施吗?不完全是
where
,而是一般的过滤功能?我确信它是可以实现的-这表明没有实现它的计划。讨论转向使用可组合查询来实现这一点,就像我的第一个示例一样。
defmodule Post do
  use Ecto.Schema

  schema "posts" do
    has_many :public_comments, Comment, where: [public: true]
  end
end