我有这个代码
defmodule Project.Search do
use Ecto.Model
defp search(query, search, order?) do
name_search = like_escape(search, ~r"(%|_)")
if String.length(search) >= 3 do
name_search = "%" <> name_search <> "%"
end
desc_search = String.replace(search, ~r"\s+", " & ")
query =
from var in query,
where: ilike(var.name, ^name_search) or
fragment("to_tsvector('english', (?->'description')::text) @@ to_tsquery('english', ?)",
var.meta, ^desc_search)
if order? do
query = from(var in query, order_by: ilike(var.name, ^name_search))
end
query
end
end
当我尝试编译时出现此错误
== Compilation error on file web/models/search.ex ==
** (Protocol.UndefinedError) protocol Enumerable not implemented for nil
(elixir) lib/enum.ex:1: Enumerable.impl_for!/1
(elixir) lib/enum.ex:112: Enumerable.reduce/3
(elixir) lib/enum.ex:1274: Enum.reduce/3
expanding macro: Ecto.Model.Dependent.__before_compile__/1
web/models/search.ex:1: Project.Search (module)
这只发生在 Phoenix 的最后一个版本上,它使用的是 0.13.0 版本
我试图改变,use Project.Web, :model
但我得到了同样的错误。