如何将 ANY 运算符与 Ecto 的数组字段一起使用?

How to use ANY operator with array field with Ecto?

如何查找带有标签 'apple' 的所有帖子?

  schema "posts" do
    field :title, :string
    field :content, :string
    field :tags, {:array, :string}

    timestamps()
  end

对于 postgresql 它将是:

SELECT title FROM posts WHERE 'apple' = ANY(tags);

您将需要使用 Ecto 的 in/2 运算符。该函数的第二个示例正是您想要的。

MyApp.Repo.all(from post in MyApp.Posts, where: ^"apple" in post.tags)