I am using Phoenix with Ecto to query a database for a single record using a primary key. All documents / examples show use as in the Phoenix controller:
def show(conn, %{"id" => id}) do
m = Repo.get(MyModel, id)
...
end
However, all parameters in Phoenix are strings, so this generates a ** (Ecto.InvalidModel) model App.MyModel failed validation when , field id had type string but type integer was expected. I worked on this in my controllers, doing something like:
def show(conn, %{"id" => id}) do
m = String.to_integer(id) |> find_my_model_by_id
...
end
defp find_my_model_by_id(id) do
Repo.get(MyModel, id)
end
The problem is that I have not seen anyone else do this type conversion. I am worried that I do not have Phoenix or Ecto configured correctly. Is there a Phoenix / Ecto convention that I lost that would automatically force my id argument to Repo.get/2to int?
Kombo source
share