Elixir 菲尼克斯:你有很多条件吗

Elixir 菲尼克斯:你有很多条件吗,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,我有以下型号: defmodule My.User do use My.Web, :model schema "users" do has_many :relationships, My.Relationship has_many :pending_friends, through: [:relationships, :friend] has_many :rejected_friends, through: [:relationships, :friend]

我有以下型号:

defmodule My.User do
  use My.Web, :model

  schema "users" do
    has_many :relationships, My.Relationship
    has_many :pending_friends, through: [:relationships, :friend]
    has_many :rejected_friends, through: [:relationships, :friend]
    has_many :friends, through: [:relationships, :friend]
    has_many :suggested_friends, through: [:relationships, :friend]

    timestamps()
  end
end


defmodule My.Relationship do
  use My.Web, :model

  schema "relationships" do
    belongs_to :user, My.User
    belongs_to :friend, My.User
    field :type, :integer

    timestamps()
  end
end
基本上,用户可以与不同类型的其他用户建立关系。 我的问题是无法正确预加载数据


我试图找到一种方法,向“has\u many”添加约束,或者编写一个查询,将“relationships”数据映射到4种类型,即通过“select”,但似乎什么都不起作用。

上次我检查时,Ecto不支持通过向
has\u many
添加条件。对于第二种方法,你写了什么问题没有达到预期效果?@YuriDymov请检查这个答案-这对你有用吗?@OleksandrAvoyants,谢谢你的评论。在发布新问题之前,我找到了这个答案,不幸的是,它对我不起作用,或者至少我做错了什么。这是一对多的情况,我的是多对多(has_many-to-many),我还不能让事情顺利进行。@Dogbert,我尝试了不同的方法,但目前我几乎不能分享任何东西,因为我不明白如何编写这样的查询。例如,“从查询中的u开始,预加载::friends”,我试着这样写:“从查询中的u开始,预加载:[friends:^(从关系中的r开始,其中:r.type==3)],但显然不起作用。为什么不使用多对多呢?很好地展示了如何使用新的EXTO。上次我检查了EXTO不支持通过
添加条件。对于第二种方法,你写了什么问题没有达到预期效果?@YuriDymov请检查这个答案-这对你有用吗?@OleksandrAvoyants,谢谢你的评论。在发布新问题之前,我找到了这个答案,不幸的是,它对我不起作用,或者至少我做错了什么。这是一对多的情况,我的是多对多(has_many-to-many),我还不能让事情顺利进行。@Dogbert,我尝试了不同的方法,但目前我几乎不能分享任何东西,因为我不明白如何编写这样的查询。例如,“从查询中的u开始,预加载::friends”,我试着这样写:“从查询中的u开始,预加载:[friends:^(从关系中的r开始,其中:r.type==3)],但显然不起作用。为什么不使用多对多呢?很好地展示了如何使用新的外星生物。