2
votes

I have this code

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

When I try to compile I got this error

== 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)

This only happen with the last version of Phoenix, It was working with version 0.13.0

I have tried to change to use Project.Web, :model but I got the same error.

1
What's the definition of "like_escape"? When you say "latest version of Phoenix" do you mean 1.0.0 or 0.17.0?Onorio Catenacci
Version 1.0.0, like_escape is a definition from Ecto.Model. I have checked the last version of Ecto.Model and I did not find the definition. I have change use Ecto.Model to import Ecto.Query, only: [from: 2] and seen that is workingGidrek
It seems the error is happening because you didn't define a "schema". Ecto should raise a nice error message here anyway.José Valim
Thank you. It was working in older versions, but I will create a schema to fix the errorGidrek

1 Answers

0
votes

I had this error on upgrading Phoenix/Ecto too, the comment above states the solution but in case this is missed:

If you just want to work with models (and not define one), you need to use:

import Ecto.Model

where you are currently using:

use Ecto.Model

use calls the __using__ macro on Ecto.Model which in turn calls use Ecto.Schema which is why you see the error here, as Ecto.Schema fails to find a schema defined in that module. You can read more about exactly what it does as of v1.0.2 here.