【发布时间】:2016-09-26 14:43:17
【问题描述】:
Ecto documentation 展示了如何进行插值。但我的查询中需要动态字段。我有几十个字段,为每个字段编写查询似乎没有凝聚力。
defmodule Hedone.SearchController do
use Hedone.Web, :controller
alias Hedone.User
def idade(conn, %{"idade+" => maior, "idade-" => menor, "campo" => campo}) do
IO.inspect campo
query = from u in User, where: u.campo > ^maior and u.campo < ^menor, select: u.name
pesquisa = Repo.all query
IO.inspect pesquisa
text conn, "Works"
end
end
此控制器产生以下错误:
** (Ecto.QueryError) web/controllers/search.ex:8: field `Hedone.User.campo` in `where` does not exist in the schema in query:
from u in Hedone.User,
where: u.campo > ^18 and u.campo < ^40,
select: u.name
自动翻译。
【问题讨论】:
标签: elixir phoenix-framework ecto